home *** CD-ROM | disk | FTP | other *** search
/ BBS in a Box 15 / BBS in a box XV-2.iso / Files II / Prog / M / MacPerl 4.13 source.sit / Perl Source ƒ / MacPerl / MPAppleEvents.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-05-04  |  135.6 KB  |  5,437 lines  |  [TEXT/MPS ]

  1. /*********************************************************************
  2. Project    :    MacPerl                -    Real Perl Application
  3. File        :    MPAppleEvents.c    -
  4. Author    :    Matthias Neeracher
  5.  
  6. A lot of this code is borrowed from 7Edit written by
  7. Apple Developer Support UK
  8.  
  9. Language    :    MPW C
  10.  
  11. $Log: MPAppleEvents.c,v $
  12. Revision 1.2  1994/05/04  02:48:27  neeri
  13. Inline input.
  14. Fix deletions.
  15.  
  16. Revision 1.1  1994/02/27  22:59:44  neeri
  17. Initial revision
  18.  
  19. Revision 0.4  1993/08/28  00:00:00  neeri
  20. FormatCommand
  21.  
  22. Revision 0.3  1993/08/16  00:00:00  neeri
  23. DoScript
  24.  
  25. Revision 0.2  1993/05/30  00:00:00  neeri
  26. Support console Windows
  27.  
  28. Revision 0.1  1993/05/29  00:00:00  neeri
  29. Compiles correctly
  30.  
  31. *********************************************************************/
  32.  
  33. #include <AppleEvents.h>
  34. #include <Sysequ.h>
  35. #include <Menus.h>
  36. #include <PLStringFuncs.h>
  37. #include <Scrap.h>
  38. #include <TextEdit.h>
  39. #include <AEObjects.h>
  40. #include <AEPackObject.h>
  41. #include <AERegistry.h>
  42. #include <Resources.h>
  43. #include <String.h>
  44. #include <TFileSpec.h>
  45.  
  46. #include "MPGlobals.h"
  47. #include "MPUtils.h"
  48. #include "MPAEUtils.h"
  49. #include "MPWindow.h"
  50. #include "MPFile.h"
  51. #include "MPAppleEvents.h"
  52. #include "MPScript.h"
  53. #include "MPSave.h"
  54. #include "MPPreferences.h"
  55.  
  56. /* these should come from the registry */
  57.  
  58. #define         kAEStartedRecording  'rec1'
  59. #define         kAEStoppedRecording    'rec0'
  60. #define         kAEDontExecute         0x00002000
  61.  
  62. #define         pText                     'TEXT'
  63. #define     cSpot                 'cspt'
  64.  
  65. /*
  66.     Text Properties
  67. */
  68.  
  69. #define         pStringWidth            'pwid'
  70.  
  71. /*
  72.     Window Properties - See the Registry for Details
  73. */
  74.  
  75. #define     pPosition               'ppos'
  76. #define         pPageSetup                'PSET' /* One of ours - Not in registry */
  77. #define         pShowBorders            'PBOR' /* Another of ours */
  78.  
  79. #define         typeTPrint                'TPNT' /* A raw TPrint record - also one of ours */
  80.  
  81. /*
  82.     Error Codes
  83. */
  84.  
  85. #define         kAEGenericErr    -1799
  86.  
  87. static short   gBigBrother;
  88. static char    *gTypingBuffer;
  89. static short   gCharsInBuffer;
  90. static AEDesc  gTypingTargetObject;
  91.  
  92. pascal Boolean AllSelected(TEHandle te)
  93. {
  94.     return ((*te)->selStart == 0 && (*te)->selEnd == (*te)->teLength);
  95. }
  96.  
  97. #ifndef RUNTIME
  98.  
  99. /*-----------------------------------------------------------------------*/
  100. /**----------                         APPLE EVENT HANDLING         ---------------**/
  101. /*-----------------------------------------------------------------------*/
  102.  
  103. pascal OSErr GetTHPrintFromDescriptor(const AEDesc *sourceDesc, THPrint *result)
  104. {
  105.     OSErr   myErr;
  106.     Size    ptSize;
  107.     AEDesc  resultDesc;
  108.  
  109.     *result = nil;
  110.  
  111.     if (myErr = AECoerceDesc(sourceDesc,typeTPrint,&resultDesc))
  112.         return myErr;
  113.  
  114.     *result = (THPrint)NewHandle(sizeof(TPrint));
  115.  
  116.     PrOpen();
  117.     PrintDefault(*result);
  118.  
  119.     HLock((Handle)*result);
  120.  
  121.     GetRawDataFromDescriptor(&resultDesc, (Ptr)**result, sizeof(TPrint), &ptSize);
  122.  
  123.     HUnlock((Handle)*result);
  124.  
  125.     if ((ptSize<sizeof(TPrint)) || (PrValidate(*result))) {
  126.         myErr = errAECoercionFail;
  127.         DisposHandle((Handle)*result);
  128.         *result = nil;
  129.     }
  130.  
  131.     PrClose();
  132.  
  133.     if (resultDesc.dataHandle)
  134.         AEDisposeDesc(&resultDesc);
  135.  
  136.     return myErr;
  137. } /*GetTHPrintFromDescriptor*/
  138.  
  139. /*******************************************************************************/
  140. /*
  141.     Object Accessors - Utility Routines
  142. */
  143.  
  144. #pragma segment ObjectAccessors
  145.  
  146. /*
  147.     Returns the WindowPtr of the window with title nameStr
  148.     or nil if there is no matching window.
  149. */
  150. pascal WindowPtr WindowNameToWindowPtr(StringPtr nameStr)
  151. {
  152.     WindowPtr theWindow;
  153.     Str255    windTitle;
  154.  
  155.     theWindow = (WindowPtr)*((Handle)WindowList);
  156.     /*
  157.         iterate through windows - we use WindowList 'cos we could
  158.         have made the window invisible and  we lose it - so we
  159.         can't set it back to visible!!
  160.     */
  161.     while (theWindow) {
  162.         GetWTitle(theWindow, windTitle);
  163.         if (EqualString(windTitle,
  164.                              nameStr,
  165.                                         false,
  166.                                         true))     /* ignore case, don't ignore diacriticals */
  167.             return theWindow;
  168.       theWindow = (WindowPtr)((WindowPeek)theWindow)->nextWindow;
  169.     }
  170.  
  171.     return theWindow;
  172. }    /* WindowNameToWindowPtr */
  173.  
  174. pascal WindowPtr GetWindowPtrOfNthWindow(short index)
  175. /* returns a ptr to the window with the given index
  176.   (front window is 1, behind that is 2, etc.).  if
  177.   there's no window with that index (inc. no windows
  178.   at all), returns nil.
  179. */
  180. {
  181.   WindowPtr theWindow;
  182.  
  183.     theWindow = (WindowPtr)*((Handle)WindowList);
  184.  
  185.     /* iterate through windows */
  186.  
  187.     while (theWindow) {
  188.         if (--index <= 0)
  189.             return theWindow;
  190.  
  191.       theWindow = (WindowPtr)((WindowPeek)theWindow)->nextWindow;
  192.     }
  193.  
  194.     return nil;
  195. }    /* GetWindowPtrOfNthWindow */
  196.  
  197. pascal short CountWindows(void)
  198. {
  199.     WindowPtr theWindow;
  200.     short     index;
  201.  
  202.     index = 0;
  203.     theWindow = (WindowPtr)*((Handle)WindowList);
  204.  
  205.     /* iterate through windows */
  206.  
  207.     while (theWindow) {
  208.         index++;
  209.         theWindow = (WindowPtr)((WindowPeek)theWindow)->nextWindow;
  210.     }
  211.  
  212.     return index;
  213. } /*CountWindows*/
  214.  
  215. #endif
  216.  
  217. /**-----------------------------------------------------------------------
  218.         Name:         DoOpenApp
  219.         Purpose:        Called on startup, creates a new document.
  220.     -----------------------------------------------------------------------**/
  221.  
  222. #pragma segment Main
  223.  
  224. pascal OSErr DoOpenApp(const AppleEvent *message, AppleEvent *reply, long refcon)
  225. {
  226.     if (gRuntimeScript)
  227.         return DoScript(message, reply, refcon);
  228.  
  229.     return noErr;
  230. }
  231.  
  232. /**-----------------------------------------------------------------------
  233.         Name:         DoOpenDocument
  234.         Purpose:        Open all the documents passed in the Open AppleEvent.
  235. -----------------------------------------------------------------------**/
  236.  
  237. #pragma segment Main
  238.  
  239. pascal OSErr DoOpenDocument(const AppleEvent *message, AppleEvent *reply, long refcon)
  240. {
  241.     long        index;
  242.     long        itemsInList;
  243.     AEKeyword   keywd;
  244.     OSErr       err;
  245.     OSErr       ignoreErr;
  246.     AEDescList  docList;
  247.     long        actSize;
  248.     DescType    typeCode;
  249.     FSSpec      theFSSpec;
  250.     EventRecord    ev;
  251.     DocType        type;
  252.  
  253.     if (gRuntimeScript)
  254.         return DoScript(message, reply, refcon);
  255.     
  256.     /* open the specified documents */
  257.  
  258.     docList.dataHandle = nil;
  259.  
  260.     err = AEGetParamDesc(message, keyDirectObject, typeAEList, &docList);
  261.  
  262.     if (err==noErr)
  263.         err = AECountItems( &docList, &itemsInList) ;
  264.     else
  265.       itemsInList = 0;
  266.  
  267.     if (itemsInList) {
  268.         err =
  269.             AEGetNthPtr(&docList, 1, typeFSS, &keywd, &typeCode, (Ptr)&theFSSpec, sizeof(theFSSpec), &actSize);
  270.         
  271.         if (!err) {
  272.             type = GetDocType(&theFSSpec);
  273.             
  274.             GetNextEvent(0, &ev);
  275.         
  276.             switch (type) {
  277.             case kPlainTextDoc:
  278.             case kOldRuntime6Doc:
  279.             case kRuntime6Doc:
  280.             case kRuntime7Doc:
  281.                 if (!(ev.modifiers & optionKey) != gPerlPrefs.runFinderOpens)
  282.                     break;
  283.                 if (refcon != -1)
  284.                     break;
  285.                     
  286.                 err = DoScript(message, reply, 0);
  287.                     
  288.                 goto done;
  289.             }
  290.         }
  291.     }
  292.  
  293.     for (index = 1; index <= itemsInList; index++)
  294.         if (err==noErr) {
  295.             err = AEGetNthPtr( &docList, index, typeFSS, &keywd, &typeCode,
  296.                                                  (Ptr)&theFSSpec, sizeof(theFSSpec), &actSize ) ;
  297.             if (err==noErr)
  298.                 switch (type = GetDocType(&theFSSpec)) {
  299.                 case kPlainTextDoc:
  300.                 case kOldRuntime6Doc:
  301.                 case kScriptDoc:
  302.                 case kRuntime6Doc:
  303.                 case kRuntime7Doc:
  304.                       err = OpenOld(theFSSpec, type);
  305.                     break;
  306.                 case kPreferenceDoc:
  307.                     OpenPreferenceFile(&theFSSpec);
  308.                     break;
  309.                 }
  310.         }
  311.  
  312. done:    
  313.   if (docList.dataHandle)
  314.         ignoreErr = AEDisposeDesc(&docList);
  315.  
  316.     return err;
  317. }
  318.  
  319. /**-----------------------------------------------------------------------
  320.         Name:         MyQuit
  321.         Purpose:        Quit event received- exit the program.
  322.     -----------------------------------------------------------------------**/
  323.  
  324. #pragma segment Main
  325.  
  326. pascal OSErr MyQuit(const AppleEvent *message, const AppleEvent *reply, long refcon)
  327. {
  328. #pragma unused (reply,refcon)
  329.  
  330.     DescType saveOpt;
  331.     OSErr    tempErr;
  332.     OSErr    myErr;
  333.     DescType returnedType;
  334.     long     actSize;
  335.  
  336.     saveOpt = kAEAsk; /* the default */
  337.     tempErr = AEGetParamPtr(message,
  338.                                     keyAESaveOptions,
  339.                                     typeEnumerated,
  340.                                     &returnedType,
  341.                                     (Ptr)&saveOpt,
  342.                                     sizeof(saveOpt),
  343.                                     &actSize);
  344.  
  345.     if (saveOpt != kAENo)
  346.         myErr = AEInteractWithUser(kAEDefaultTimeout, nil, nil);
  347.  
  348.     if (myErr == noErr)
  349.         DoQuit(saveOpt);
  350.  
  351.     return myErr;
  352. }
  353.  
  354. /**-----------------------------------------------------------------------
  355.         Name:         DoAppleEvent
  356.         Purpose:        Process and despatch the AppleEvent
  357.     -----------------------------------------------------------------------**/
  358.  
  359. #pragma segment Main
  360.  
  361. pascal void DoAppleEvent(EventRecord theEvent)
  362. {
  363.   OSErr err;
  364.  
  365.   /*should check for your own event message types here - if you have any*/
  366.  
  367.     err = AEProcessAppleEvent(&theEvent);
  368. }
  369.  
  370. #ifndef RUNTIME
  371.  
  372. /**-----------------------------------------------------------------------
  373.         Name:         MakeSelfAddress
  374.         Purpose:        Builds an AEAddressDesc for the current process
  375.     -----------------------------------------------------------------------**/
  376.  
  377. pascal OSErr MakeSelfAddress(AEAddressDesc *selfAddress)
  378. {
  379.   ProcessSerialNumber procSerNum;
  380.  
  381.     procSerNum.highLongOfPSN = 0;
  382.     procSerNum.lowLongOfPSN  = kCurrentProcess;
  383.  
  384.     return
  385.         AECreateDesc(
  386.             typeProcessSerialNumber,
  387.             (Ptr)&procSerNum,
  388.             sizeof(procSerNum),
  389.             selfAddress);
  390. } /* MakeSelfAddress */
  391.  
  392. #endif
  393.  
  394. /**--------------------------------------------------------------------
  395.     Name :         SendAESetObjProp
  396.     Function :     Creates a property object from an object,
  397.                     a property type and its data and sends it to
  398.                     the requested address, and cleans up zapping params too
  399.     --------------------------------------------------------------------**/
  400.  
  401. pascal OSErr SendAESetObjProp(
  402.     AEDesc        *theObj,
  403.     DescType      theProp,
  404.     AEDesc        *theData,
  405.     AEAddressDesc *toWhom)
  406. {
  407.     AEDesc     propObjSpec;
  408.     AppleEvent myAppleEvent;
  409.     AppleEvent defReply;
  410.     OSErr      myErr;
  411.     OSErr      ignoreErr;
  412.     AEDesc     theProperty;
  413.  
  414.     /* create an object spec that represents the property of the given object */
  415.  
  416.     myErr = AECreateDesc(typeType, (Ptr)&theProp, sizeof(theProp), &theProperty);
  417.     if (myErr==noErr)
  418.         myErr =
  419.             CreateObjSpecifier(
  420.                 cProperty,
  421.                 theObj,
  422.                 formPropertyID,
  423.                 &theProperty,
  424.                 true,
  425.                 &propObjSpec);
  426.  
  427.     /* create event */
  428.  
  429.     if (myErr==noErr)
  430.         myErr =
  431.             AECreateAppleEvent(
  432.                 kAECoreSuite,
  433.                 kAESetData,
  434.                 toWhom,
  435.                 0,
  436.                 0,
  437.                 &myAppleEvent);
  438.  
  439.     /* add prop obj spec to the event */
  440.  
  441.     if (myErr==noErr)
  442.         myErr = AEPutParamDesc(&myAppleEvent, keyDirectObject, &propObjSpec);
  443.  
  444.     /* add prop data to the event */
  445.  
  446.     if (myErr==noErr)
  447.         myErr = AEPutParamDesc(&myAppleEvent, keyAEData, theData);
  448.  
  449.     /* send event */
  450.  
  451.     if (myErr==noErr)
  452.         myErr =
  453.             AESend(
  454.                 &myAppleEvent,
  455.                 &defReply,
  456.                 kAENoReply+kAEAlwaysInteract,
  457.                 kAENormalPriority,
  458.                 kAEDefaultTimeout,
  459.                 nil,
  460.                 nil);
  461.  
  462.     if (myAppleEvent.dataHandle)
  463.         ignoreErr = AEDisposeDesc(&myAppleEvent);
  464.  
  465.     if (&propObjSpec.dataHandle)
  466.       ignoreErr = AEDisposeDesc(&propObjSpec);
  467.  
  468.     if (theData->dataHandle)
  469.         ignoreErr = AEDisposeDesc(theData);
  470.  
  471.     if (toWhom->dataHandle)
  472.         ignoreErr = AEDisposeDesc(toWhom);
  473.  
  474.     return myErr;
  475. }    /* SendAESetObjProp */
  476.  
  477. /*----------------------------------------------------------------------------------------------*/
  478. /*
  479.     Private AEObject definitions
  480. */
  481. #pragma segment AECommandHandlers
  482.  
  483. #define typeMyAppl       'BAPP'    /* sig of my private token type for the app     - appToken   */
  484. #define typeMyWndw         'BWIN'    /* sig of my private token type for windows     - windowToken   */
  485. #define typeMyText           'BTXT'    /* sig of my private token type for text        - textToken     */
  486. #define typeMyTextProp   'BPRP'    /* sig of my private token type for text properties    - textPropToken */
  487. #define typeMyWindowProp 'WPRP'    /* sig of my private token type for window properties  - windowPropToken */
  488. #define typeMyApplProp   'APRP'    /* sig of my private token type for appl properties    - applPropToken */
  489. #define typeMyMenu       'MTKN'    /* sig of my private token type for menus       - menuToken  */
  490. #define typeMyMenuItem   'ITKN'    /* sig of my private token type for menus       - menuItemToken  */
  491. #define typeMyMenuProp   'MPRP'    /* sig of my private token type for menu properties - menuPropToken  */
  492. #define typeMyItemProp   'IPRP'    /* sig of my private token type for menu item properties  - menuItemPropToken  */
  493.  
  494. /* These are entirely private to our app - used only when resolving the object specifier */
  495.  
  496. typedef    ProcessSerialNumber appToken;
  497.  
  498. struct applPropToken{
  499.     appToken tokenApplToken;
  500.     DescType tokenApplProperty;
  501. };
  502.  
  503. typedef struct applPropToken applPropToken;
  504.  
  505. typedef    WindowPtr WindowToken;
  506.  
  507. struct windowPropToken{
  508.         WindowToken tokenWindowToken;
  509.         DescType    tokenProperty;
  510.     };
  511.  
  512. typedef struct windowPropToken windowPropToken;
  513.  
  514. struct TextToken{
  515.         WindowPtr tokenWindow;
  516.         short     tokenOffset;
  517.         short     tokenLength;
  518.     };
  519.  
  520. typedef struct TextToken TextToken;
  521.  
  522. struct textPropToken{
  523.         TextToken propertyTextToken;
  524.         DescType  propertyProperty;
  525.     };
  526.  
  527. typedef struct textPropToken textPropToken;
  528.  
  529. /* Tokens related to menus */
  530.  
  531. struct MenuToken {
  532.     MenuHandle theTokenMenu;
  533.     short      theTokenID;
  534. };
  535.  
  536. typedef struct MenuToken MenuToken;
  537.  
  538. struct MenuItemToken {
  539.     MenuToken  theMenuToken;
  540.     short      theTokenItem;
  541. };
  542.  
  543. typedef struct MenuItemToken MenuItemToken;
  544.  
  545. struct MenuPropToken {
  546.     MenuToken  theMenuToken;
  547.     DescType   theMenuProp;
  548. };
  549.  
  550. typedef struct MenuPropToken MenuPropToken;
  551.  
  552. struct MenuItemPropToken {
  553.     MenuItemToken  theItemToken;
  554.     DescType       theItemProp;
  555. };
  556.  
  557. typedef struct MenuItemPropToken MenuItemPropToken;
  558.  
  559. #ifndef RUNTIME
  560.  
  561. /*
  562.     Name: GotRequiredParams
  563.     Function: Checks all parameters defined as 'required' have been read
  564. */
  565. pascal OSErr GotRequiredParams(const AppleEvent *theAppleEvent)
  566. {
  567.     OSErr    myErr;
  568.     DescType returnedType;
  569.     Size     actSize;
  570.  
  571.     /* look for the keyMissedKeywordAttr, just to see if it's there */
  572.  
  573.     myErr =
  574.         AEGetAttributePtr(
  575.             theAppleEvent,
  576.             keyMissedKeywordAttr,
  577.             typeWildCard,
  578.             &returnedType,
  579.             nil,
  580.             0,
  581.             &actSize);
  582.  
  583.     if (myErr == errAEDescNotFound)
  584.         return noErr;            /* attribute not there means we got all req params */
  585.     else
  586.         if (myErr == noErr)
  587.             return errAEParamMissed;        /* attribute there means missed at least one */
  588.         else
  589.             return myErr;        /* some unexpected arror in looking for the attribute */
  590. }    /* GotReqiredParams */
  591.  
  592. /**--------------------------------------------------------------------
  593.     Name : SetSelectionOfAppleEventDirectObject
  594.     Function : Resolves the Direct Object into a text token and
  595.                          sets the selection of the specified document to that
  596.                          specified in the direct object.
  597.                          Returns the doc and TEHandle chosen.
  598.     --------------------------------------------------------------------**/
  599.  
  600. pascal OSErr SetSelectionOfAppleEventDirectObject(
  601.     const AppleEvent *theAppleEvent,
  602.     DPtr             *theDocument,
  603.     TEHandle         *theHTE)
  604. {
  605.     OSErr     myErr;
  606.     DescType  returnedType;
  607.     long      actSize;
  608.     TextToken myTextToken;
  609.     OSErr     paramErr;
  610.     WindowPtr fWin;
  611.  
  612.     paramErr =
  613.         AEGetParamPtr(
  614.             theAppleEvent,
  615.             keyDirectObject,
  616.             typeMyText,
  617.             &returnedType,
  618.             (Ptr)&myTextToken,
  619.             sizeof(myTextToken),
  620.             &actSize);
  621.  
  622.     myErr = GotRequiredParams(theAppleEvent);
  623.  
  624.     /* now let's work on the direct object, if any */
  625.  
  626.     if (paramErr == errAEDescNotFound) {
  627.         /* no direct object; check we have a window */
  628.  
  629.         fWin = FrontWindow();
  630.  
  631.         if (fWin == nil)
  632.             return -1700; /* Generic Err */
  633.  
  634.         *theDocument = DPtrFromWindowPtr(fWin);
  635.         *theHTE      = (*theDocument)->theText;
  636.     }
  637.  
  638.     if (paramErr == noErr)  {
  639.         /* got a text token */
  640.  
  641.         *theDocument = DPtrFromWindowPtr(myTextToken.tokenWindow);
  642.         *theHTE      = (*theDocument)->theText;
  643.  
  644.         TESetSelect(
  645.             myTextToken.tokenOffset-1,
  646.             myTextToken.tokenOffset+myTextToken.tokenLength-1,
  647.             *theHTE);
  648.  
  649.     }
  650.  
  651.     if ((paramErr!=noErr) &&
  652.          (paramErr!=errAEDescNotFound)
  653.     ) {
  654.          *theDocument = DPtrFromWindowPtr(FrontWindow());
  655.          *theHTE      = (*theDocument)->theText;
  656.      }
  657.  
  658.     return myErr;
  659.  
  660. } /* SetSelectionOfAppleEventDirectObject */
  661.  
  662. /**--------------------------------------------------------------------
  663.     Name             : SetSelectionOfAppleEventObject
  664.     Function     : Resolves the whatObject type of the AppleEvent into a text
  665.                       token and sets the selection to be that specified in the
  666.                       text token.
  667.                       Returns the doc and TEHandle chosen.
  668.     --------------------------------------------------------------------**/
  669.  
  670. pascal OSErr SetSelectionOfAppleEventObject(
  671.     OSType            whatObject,
  672.     const AppleEvent *theAppleEvent,
  673.     DPtr             *theDocument,
  674.     TEHandle         *theHTE)
  675. {
  676.     DescType   returnedType;
  677.     long       actSize;
  678.     TextToken  myTextToken;
  679.     OSErr      paramErr;
  680.  
  681.     paramErr  =
  682.         AEGetParamPtr(
  683.             theAppleEvent,
  684.             whatObject,
  685.             typeMyText,
  686.             &returnedType,
  687.             (Ptr)&myTextToken,
  688.             sizeof(myTextToken),
  689.             &actSize);
  690.  
  691.     if (paramErr == noErr) {
  692.         /* got a text token */
  693.  
  694.         *theDocument = DPtrFromWindowPtr(myTextToken.tokenWindow);
  695.         *theHTE      = (*theDocument)->theText;
  696.  
  697.         TESetSelect(
  698.             myTextToken.tokenOffset-1,
  699.             myTextToken.tokenOffset+myTextToken.tokenLength-1,
  700.             *theHTE);
  701.     }
  702.  
  703.     return paramErr;
  704. } /* SetSelectionOfAppleEventObject */
  705.  
  706. #endif
  707.  
  708. pascal void EnforceMemory(DPtr theDocument, TEHandle theHTE)
  709. {
  710.     if (theDocument->kind != kDocumentWindow && (*theHTE)->teLength > theDocument->u.cons.memory) {
  711.         short    obulus =    (*theHTE)->teLength - theDocument->u.cons.memory;
  712.         short saveStart;
  713.         short    saveEnd;
  714.         Ptr    search = *(*theHTE)->hText;
  715.         short rest   = theDocument->u.cons.memory;
  716.         
  717.         while (search[obulus-1] != '\n' && rest--)
  718.             ++obulus;
  719.             
  720.         saveStart    =    (*theHTE)->selStart - obulus;
  721.         saveEnd        =    (*theHTE)->selEnd      - obulus;
  722.         
  723.         TESetSelect(0, obulus, theHTE);
  724.         TEDelete(theHTE);
  725.         TESetSelect(saveStart < 0 ? 0 : saveStart, saveEnd < 0 ? 0 : saveEnd, theHTE);
  726.         
  727.         if (theDocument->u.cons.fence < 32767)
  728.             theDocument->u.cons.fence    -=    obulus;
  729.     }
  730. }
  731.  
  732. #ifndef RUNTIME
  733.  
  734. /* -----------------------------------------------------------------------
  735.         Name:         DoCopyEdit
  736.         Purpose:        Performs a copy text operation on the text selection specified
  737.                         by the appleEvent direct object (if any)
  738.      -----------------------------------------------------------------------**/
  739.  
  740. pascal OSErr DoCopyEdit(const AppleEvent *theAppleEvent,AppleEvent *reply, long refCon)
  741. {
  742. #pragma unused (reply,refCon)
  743.  
  744.     OSErr    myErr;
  745.     TEHandle theHTE;
  746.     DPtr     theDocument;
  747.  
  748.     /*
  749.             Here we extract the information about what to copy from the
  750.             directObject - if any
  751.     */
  752.  
  753.     if (myErr = SetSelectionOfAppleEventDirectObject(theAppleEvent,&theDocument,&theHTE))
  754.         return myErr;
  755.  
  756.     myErr = (OSErr) ZeroScrap();
  757.     TECopy(theHTE);
  758.     TEToScrap();
  759.  
  760.     if (myErr)
  761.         return myErr;
  762.  
  763.     if (!SetSelectionOfAppleEventObject(
  764.         keyAEContainer,
  765.         theAppleEvent,
  766.         &theDocument,
  767.         &theHTE)
  768.     ) {
  769.         if (theDocument->kind == kDocumentWindow) {
  770.             DoTEPasteSectionRecalc(theDocument);
  771.         } else if (!theDocument->u.cons.selected || (*theHTE)->selStart < theDocument->u.cons.fence) {
  772.             SysBeep(1);
  773.             
  774.             return errAEEventNotHandled;
  775.         }
  776.             
  777.         TEFromScrap();
  778.         TEPaste(theHTE);
  779.         EnforceMemory(theDocument, theHTE);
  780.         AdjustScrollbars(theDocument, false);
  781.         DrawPageExtras(theDocument);
  782.             
  783.         theDocument->dirty = true;
  784.     }
  785.  
  786.     return noErr;
  787. } /* DoCopyEdit */
  788.  
  789. /* -----------------------------------------------------------------------
  790.         Name:             DoCutEdit
  791.         Purpose:        Performs a cut text operation on the current text selection
  792.      -----------------------------------------------------------------------**/
  793.  
  794. pascal OSErr DoCutEdit(const AppleEvent *theAppleEvent, AppleEvent *reply, long refCon)
  795. {
  796. #pragma unused (reply,refCon)
  797.  
  798.     OSErr    myErr;
  799.     TEHandle theHTE;
  800.     DPtr     theDocument;
  801.  
  802.     if (myErr = SetSelectionOfAppleEventDirectObject(theAppleEvent,&theDocument,&theHTE))
  803.         return myErr;
  804.  
  805.     if (theDocument->kind == kDocumentWindow) {
  806.         DoTECutSectionRecalc(theDocument);
  807.         
  808.         theDocument->dirty = true;
  809.     } else if (!theDocument->u.cons.selected || (*theHTE)->selStart < theDocument->u.cons.fence) {
  810.         if (AllSelected(theHTE)) {
  811.             if (theDocument->u.cons.fence < 32767)
  812.                 theDocument->u.cons.fence = 0;
  813.         } else {
  814.             SysBeep(1);
  815.         
  816.             return DoCopyEdit(theAppleEvent, reply, refCon);
  817.         }
  818.     }
  819.  
  820.     myErr = (OSErr) ZeroScrap();
  821.     TECut(theHTE);
  822.     TEToScrap();
  823.     AdjustScrollbars(theDocument, false);
  824.     DrawPageExtras(theDocument);
  825.  
  826.     return myErr;
  827. } /* DoCutEdit */
  828.  
  829. /* -----------------------------------------------------------------------
  830.         Name:         DoPasteEdit
  831.         Purpose:        Performs a paste text operation on the text selection specified
  832.                         by the appleEvent direct object (if any)
  833.      -----------------------------------------------------------------------**/
  834.  
  835. pascal OSErr DoPasteEdit(const AppleEvent *theAppleEvent, AppleEvent *reply, long refCon)
  836. {
  837. #pragma unused (reply,refCon)
  838.  
  839.     OSErr    myErr;
  840.     TEHandle theHTE;
  841.     DPtr     theDocument;
  842.  
  843.     if (myErr = SetSelectionOfAppleEventDirectObject(theAppleEvent, &theDocument, &theHTE))
  844.         return myErr;
  845.  
  846.     if (theDocument->kind == kDocumentWindow) {
  847.         DoTEPasteSectionRecalc(theDocument);
  848.     } else if (!theDocument->u.cons.selected || (*theHTE)->selStart < theDocument->u.cons.fence) {
  849.         SysBeep(1);
  850.             
  851.         return errAEEventNotHandled;
  852.     }
  853.     
  854.     TEFromScrap();
  855.     TEPaste(theHTE);
  856.     EnforceMemory(theDocument, theHTE);
  857.     AdjustScrollbars(theDocument, false);
  858.     DrawPageExtras(theDocument);
  859.         
  860.     theDocument->dirty = true;
  861.  
  862.     return noErr;
  863. } /* DoPasteEdit */
  864.  
  865. /* -----------------------------------------------------------------------
  866.         Name:         DoDeleteEdit
  867.         Purpose:        Performs a delete text operation on the selection specified
  868.                         by the appleEvent direct object (if any)
  869.      -----------------------------------------------------------------------**/
  870.  
  871. pascal OSErr DoDeleteEdit(const AppleEvent *theAppleEvent, AppleEvent *reply, long refcon)
  872. {
  873. #pragma unused (reply,refcon)
  874.  
  875.     OSErr     myErr;
  876.     TEHandle theHTE;
  877.     DPtr     theDocument;
  878.  
  879.     if (myErr = SetSelectionOfAppleEventDirectObject(theAppleEvent, &theDocument, &theHTE))
  880.         return myErr;
  881.  
  882.     if (theDocument->kind == kDocumentWindow) {
  883.         DoTEDeleteSectionRecalc(theDocument);
  884.     } else if (!theDocument->u.cons.selected || (*theHTE)->selStart < theDocument->u.cons.fence) {
  885.         if (AllSelected(theHTE)) {
  886.             if (theDocument->u.cons.fence < 32767)
  887.                 theDocument->u.cons.fence = 0;
  888.         } else {
  889.             SysBeep(1);
  890.                 
  891.             return errAEEventNotHandled;
  892.         }
  893.         theDocument->u.cons.fence = 0;
  894.     }
  895.     
  896.     TEDelete(theHTE);
  897.     AdjustScrollbars(theDocument, false);
  898.     DrawPageExtras(theDocument);
  899.     theDocument->dirty = true;
  900.  
  901.     return noErr;
  902. } /*DoDeleteEdit*/
  903.  
  904. #endif
  905.  
  906. void RecalcFontInfo(TEHandle te)
  907. {
  908.     TEPtr        t;
  909.     short        oldFont;
  910.     short        oldSize;
  911.     FontInfo    info;
  912.  
  913.     HLock((Handle) te);
  914.  
  915.     t             = *te;
  916.     oldFont    =    t->inPort->txFont;
  917.     oldSize    =    t->inPort->txSize;
  918.  
  919.     SetPort(t->inPort);
  920.     TextFont(t->txFont);
  921.     TextSize(t->txSize);
  922.     GetFontInfo(&info);
  923.     TextFont(oldFont);
  924.     TextSize(oldSize);
  925.  
  926.     t->lineHeight    =    info.ascent+info.descent+info.leading;
  927.     t->fontAscent    =    info.ascent;
  928.     InvalRect(&t->viewRect);
  929.     HUnlock((Handle) te);
  930.  
  931.     TECalText(te);
  932. }
  933.  
  934. #ifndef RUNTIME
  935.  
  936. /* -----------------------------------------------------------------------
  937.         Name:         SetWindowProperty
  938.         Purpose:        Sets the window property specified in theWindowPropToken to
  939.                         be that supplied in dataDesc.
  940.      -----------------------------------------------------------------------**/
  941.  
  942. pascal OSErr SetWindowProperty(const AEDesc *theWPTokenDesc, const AEDesc *dataDesc)
  943. {
  944.       Str255          name;
  945.     DPtr            theDocument;
  946.     short                 size;
  947.     short                 font;
  948.     OSErr           err;
  949.     OSErr           ignoreErr;
  950.     Rect            thePosnRect;
  951.     Boolean         theBoolean;
  952.     TEHandle        theHTE;
  953.     GrafPtr         oldPort;
  954.     Point           thePosn;
  955.     THPrint         theTHPrint;
  956.     windowPropToken theWindowPropToken;
  957.     AEDesc          newDesc;
  958.     AEDesc          tokenDesc;
  959.     Size            tokenSize;
  960.     TextToken       myTextToken;
  961.     short           hOffset;
  962.     short           vOffset;
  963.  
  964.     if (err = AECoerceDesc(theWPTokenDesc, typeMyWindowProp, &newDesc))
  965.         return err;
  966.  
  967.     GetRawDataFromDescriptor(
  968.         &newDesc,
  969.         (Ptr)&theWindowPropToken,
  970.         sizeof(theWindowPropToken),
  971.         &tokenSize);
  972.  
  973.     err = AEDisposeDesc(&newDesc);
  974.  
  975.     GetPort(&oldPort);
  976.     SetPort(theWindowPropToken.tokenWindowToken);
  977.  
  978.     theDocument = DPtrFromWindowPtr(theWindowPropToken.tokenWindowToken);
  979.  
  980.     switch (theWindowPropToken.tokenProperty) {
  981.     case pName:
  982.         err = GetPStringFromDescriptor(dataDesc, (char *)name);
  983.         if (err==noErr)
  984.             if (theDocument->kind != kDocumentWindow)
  985.                 return errAEEventNotHandled;
  986.             else if (name[0] == 0)
  987.                 err = errAEWrongDataType;
  988.             else {
  989.                 SetWTitle(theWindowPropToken.tokenWindowToken, name);
  990.                 PLstrcpy(theDocument->theFileName, name); /* Should we do this??? */
  991.                 theDocument->dirty = true;
  992.             }
  993.         break;
  994.  
  995.     case pText:
  996.         theHTE = theDocument->theText;
  997.         TESetSelect(0, 32000, theHTE);
  998.  
  999.         if (theDocument->kind == kDocumentWindow) {
  1000.             DoTEDeleteSectionRecalc(theDocument);
  1001.         } else if (!theDocument->u.cons.selected || (*theHTE)->selStart < theDocument->u.cons.fence) {
  1002.             SysBeep(1);
  1003.             
  1004.             return errAEEventNotHandled;
  1005.         }
  1006.         
  1007.         TEDelete(theHTE);
  1008.         GetTextFromDescIntoTEHandle(dataDesc, theHTE);
  1009.         EnforceMemory(theDocument, theHTE);
  1010.         
  1011.         theDocument->dirty = true;
  1012.         break;
  1013.  
  1014.     case pBounds:
  1015.         err = GetRectFromDescriptor(dataDesc, &thePosnRect);
  1016.         /* the rectangle is for the structure region, and is in global coordinates */
  1017.         /* MoveWindow and SizeWindow apply to the content region, so we have to massage a little */
  1018.  
  1019.         thePosnRect.top    += (**((WindowPeek)theWindowPropToken.tokenWindowToken)->contRgn).rgnBBox.top -
  1020.                                      (**((WindowPeek)theWindowPropToken.tokenWindowToken)->strucRgn).rgnBBox.top;
  1021.  
  1022.         thePosnRect.left   += (**((WindowPeek)theWindowPropToken.tokenWindowToken)->contRgn).rgnBBox.left -
  1023.                                      (**((WindowPeek)theWindowPropToken.tokenWindowToken)->strucRgn).rgnBBox.left;
  1024.  
  1025.         thePosnRect.bottom += (**((WindowPeek)theWindowPropToken.tokenWindowToken)->contRgn).rgnBBox.bottom -
  1026.                                      (**((WindowPeek)theWindowPropToken.tokenWindowToken)->strucRgn).rgnBBox.bottom;
  1027.  
  1028.         thePosnRect.right  += (**((WindowPeek)theWindowPropToken.tokenWindowToken)->contRgn).rgnBBox.right -
  1029.                                      (**((WindowPeek)theWindowPropToken.tokenWindowToken)->strucRgn).rgnBBox.right;
  1030.  
  1031.         if (EmptyRect(&thePosnRect))
  1032.             err = errAECorruptData;
  1033.         else {
  1034.             MoveWindow(
  1035.                 theWindowPropToken.tokenWindowToken,
  1036.                 thePosnRect.left,
  1037.                 thePosnRect.top,
  1038.                 false);
  1039.             SizeWindow(
  1040.                 theWindowPropToken.tokenWindowToken,
  1041.                 thePosnRect.right- thePosnRect.left,
  1042.                 thePosnRect.bottom-thePosnRect.top,
  1043.                 true);
  1044.             ResizeWindow(theDocument);
  1045.         }
  1046.         break;
  1047.  
  1048.     case pPosition:
  1049.         err = GetPointFromDescriptor(dataDesc, &thePosn);
  1050.         /* the point is for the structure region, and is in global coordinates */
  1051.         /* MoveWindow applies to the content region, so we have to massage a little */
  1052.  
  1053.         hOffset = (**((WindowPeek)theWindowPropToken.tokenWindowToken)->contRgn).rgnBBox.left -
  1054.                      (**((WindowPeek)theWindowPropToken.tokenWindowToken)->strucRgn).rgnBBox.left;
  1055.  
  1056.         vOffset = (**((WindowPeek)theWindowPropToken.tokenWindowToken)->contRgn).rgnBBox.top -
  1057.                      (**((WindowPeek)theWindowPropToken.tokenWindowToken)->strucRgn).rgnBBox.top;
  1058.  
  1059.         thePosn.v  += vOffset;
  1060.         thePosn.h  += hOffset;
  1061.  
  1062.         MoveWindow(
  1063.             theWindowPropToken.tokenWindowToken,
  1064.             thePosn.h,
  1065.             thePosn.v,
  1066.             false);
  1067.  
  1068.         ResizeWindow(theDocument);
  1069.         break;
  1070.  
  1071.     case pIsZoomed:
  1072.         err = GetBooleanFromDescriptor(dataDesc, &theBoolean);
  1073.         if (theBoolean)
  1074.             ZoomWindow(qd.thePort, inZoomOut, false);
  1075.         else
  1076.             ZoomWindow(qd.thePort, inZoomIn, false);
  1077.  
  1078.         ResizeWindow(theDocument);
  1079.         break;
  1080.  
  1081.     case pVisible:
  1082.         err = GetBooleanFromDescriptor(dataDesc, &theBoolean);
  1083.         if (theBoolean)
  1084.             DoShowWindow(theWindowPropToken.tokenWindowToken);
  1085.         else
  1086.             DoHideWindow(theWindowPropToken.tokenWindowToken);
  1087.         break;
  1088.  
  1089.     case pPageSetup:
  1090.         err = GetTHPrintFromDescriptor(dataDesc, &theTHPrint);
  1091.  
  1092.         if (theTHPrint) {
  1093.             if (theDocument->thePrintSetup)
  1094.                 DisposHandle((Handle)theDocument->thePrintSetup);
  1095.  
  1096.             theDocument->thePrintSetup = theTHPrint;
  1097.  
  1098.             ResizePageSetupForDocument(theDocument);
  1099.         }
  1100.         break;
  1101.  
  1102.     case pShowBorders:
  1103.         if (theDocument->kind != kDocumentWindow)
  1104.             return errAEEventNotHandled;
  1105.             
  1106.         err = GetBooleanFromDescriptor(dataDesc, &theBoolean);
  1107.         theDocument->u.reg.showBorders = theBoolean;
  1108.         if (theBoolean)
  1109.             DrawPageExtras(theDocument); /* Does the clipping as well as drawing borders/page breaks */
  1110.         else
  1111.             InvalidateDocument(theDocument);
  1112.         break;
  1113.  
  1114.     case pFont:
  1115.         err = GetPStringFromDescriptor(dataDesc, (char *)name);
  1116.         GetFNum(name, &font);
  1117.     
  1118.         (*theDocument->theText)->txFont = font;
  1119.         RecalcFontInfo(theDocument->theText);
  1120.         AdjustScrollbars(theDocument, false);
  1121.         DrawPageExtras(theDocument);
  1122.         
  1123.         if (theDocument->kind == kDocumentWindow)
  1124.             theDocument->dirty = true;
  1125.     
  1126.         if (theDocument->theWindow == FrontWindow() && !gInBackground)
  1127.             AdjustScript(theDocument);
  1128.  
  1129.         break;
  1130.  
  1131.     case pPointSize:
  1132.         err = GetIntegerFromDescriptor(dataDesc, &size);
  1133.  
  1134.         (*theDocument->theText)->txSize = size;
  1135.         RecalcFontInfo(theDocument->theText);
  1136.         AdjustScrollbars(theDocument, false);
  1137.         DrawPageExtras(theDocument);
  1138.         
  1139.         if (theDocument->kind == kDocumentWindow)
  1140.             theDocument->dirty = true;
  1141.  
  1142.         if (theDocument->theWindow == FrontWindow() && !gInBackground)
  1143.             AdjustScript(theDocument);
  1144.  
  1145.         break;
  1146.  
  1147.     case pSelection:
  1148.         err = AECoerceDesc(dataDesc, typeMyText, &tokenDesc);
  1149.  
  1150.         GetRawDataFromDescriptor(&tokenDesc,
  1151.                                                          (Ptr)&myTextToken,
  1152.                                                          sizeof(myTextToken),
  1153.                                                          &tokenSize);
  1154.  
  1155.         ignoreErr = AEDisposeDesc(&tokenDesc);
  1156.  
  1157.         if (err == noErr) {
  1158.             /* got a text token */
  1159.  
  1160.             theDocument = DPtrFromWindowPtr(myTextToken.tokenWindow);
  1161.             theHTE      = theDocument->theText;
  1162.  
  1163.             TESetSelect(
  1164.                 myTextToken.tokenOffset-1,
  1165.                 myTextToken.tokenOffset+myTextToken.tokenLength-1,
  1166.                 theHTE);
  1167.         }
  1168.         break;
  1169.  
  1170.     case pIndex:
  1171.     case pIsModal:
  1172.     case pIsResizable:
  1173.     case pHasTitleBar:
  1174.     case pHasCloseBox:
  1175.     case pIsFloating:
  1176.     case pIsZoomable:
  1177.     case pIsModified:
  1178.         err = errAEEventNotHandled; /* We don't allow these to be set */
  1179.         break;
  1180.     }
  1181.     
  1182.     SetPort(oldPort);
  1183.  
  1184.     return err;
  1185. } /* SetWindowProperty */
  1186.  
  1187. /* -----------------------------------------------------------------------
  1188.         Name:         SetTextProperty
  1189.         Purpose:        Sets the text property specfied by theTextPropToken to
  1190.                         that in dataDesc.
  1191.      -----------------------------------------------------------------------**/
  1192.  
  1193. pascal OSErr SetTextProperty(const AEDesc *tokenDesc, const AEDesc *dataDesc)
  1194. {
  1195.     TEHandle      theHTE;
  1196.     DPtr          theDoc;
  1197.     OSErr         myErr;
  1198.     textPropToken theTextPropToken;
  1199.     AEDesc        newDesc;
  1200.     Size          tokenSize;
  1201.  
  1202.     newDesc.dataHandle = nil;
  1203.  
  1204.     if (myErr = AECoerceDesc(tokenDesc, typeMyTextProp, &newDesc))
  1205.         return myErr;
  1206.  
  1207.     GetRawDataFromDescriptor(&newDesc, (Ptr)&theTextPropToken, sizeof(theTextPropToken), &tokenSize);
  1208.     myErr             =    AEDisposeDesc(&newDesc);
  1209.     theDoc             =    DPtrFromWindowPtr(theTextPropToken.propertyTextToken.tokenWindow);
  1210.     
  1211.     if (theDoc->kind == kDocumentWindow)
  1212.         theDoc->dirty     =    true;
  1213.  
  1214.     if (theTextPropToken.propertyProperty == pText) {
  1215.         theHTE = theDoc->theText;
  1216.         TESetSelect(
  1217.             theTextPropToken.propertyTextToken.tokenOffset-1,
  1218.             theTextPropToken.propertyTextToken.tokenOffset+theTextPropToken.propertyTextToken.tokenLength-1,
  1219.             theHTE);
  1220.         
  1221.         if (theDoc->kind == kDocumentWindow) {
  1222.             DoTEDeleteSectionRecalc(theDoc);
  1223.         } else if (!theDoc->u.cons.selected || (*theHTE)->selStart < theDoc->u.cons.fence) {
  1224.             SysBeep(1);
  1225.             
  1226.             return errAEEventNotHandled;
  1227.         }
  1228.         
  1229.         TEDelete(theHTE);
  1230.         myErr = GetTextFromDescIntoTEHandle(dataDesc, theHTE);
  1231.         EnforceMemory(theDoc, theHTE);
  1232.             
  1233.         theDoc->dirty = true;
  1234.         
  1235.         return myErr;
  1236.     }
  1237.  
  1238.     return errAEWrongDataType;
  1239. } /* SetTextProperty */
  1240.  
  1241. /* -----------------------------------------------------------------------
  1242.         Name:         HandleSetData
  1243.         Purpose:        Resolves the object into a token (could be one of many) and
  1244.                         the sets the data of that object to dataDesc.
  1245.      -----------------------------------------------------------------------**/
  1246.  
  1247. pascal OSErr HandleSetData(const AEDesc *theObj, const AEDesc *dataDesc)
  1248. {
  1249.     OSErr           myErr;
  1250.     AEDesc          newDesc;
  1251.     DPtr            theDocument;
  1252.     TEHandle        theHTE;
  1253.     TextToken       theTextToken;
  1254.     Size            tokenSize;
  1255.     AEDesc          objTokenDesc;
  1256.     OSErr           ignoreErr;
  1257.  
  1258.     objTokenDesc.dataHandle = nil;
  1259.     newDesc.dataHandle      = nil;
  1260.  
  1261.     /*
  1262.         Coerce theObj into a token which we can use -
  1263.              set the property or data for that token
  1264.     */
  1265.  
  1266.     myErr = AEResolve(theObj ,kAEIDoMinimum, &objTokenDesc);
  1267.  
  1268.     /* We don't actually allow ANY app property setting, but
  1269.         just incase we'll decode looking for an typeMyApplProp and flag an error -
  1270.          do same for menu related tokens
  1271.     */
  1272.  
  1273.     if (
  1274.         (objTokenDesc.descriptorType == typeMyApplProp) ||
  1275.         (objTokenDesc.descriptorType == typeMyMenu    ) ||
  1276.         (objTokenDesc.descriptorType == typeMyMenuProp) ||
  1277.         (objTokenDesc.descriptorType == typeMyMenuItem) ||
  1278.         (objTokenDesc.descriptorType == typeMyItemProp)
  1279.     )
  1280.         myErr = errAEWrongDataType;
  1281.     else if (objTokenDesc.descriptorType == typeMyWindowProp)
  1282.         myErr = SetWindowProperty(&objTokenDesc, dataDesc);
  1283.     else if (objTokenDesc.descriptorType == typeMyTextProp)
  1284.         myErr = SetTextProperty(&objTokenDesc, dataDesc);
  1285.     else if (objTokenDesc.descriptorType == typeMyText)
  1286.         if (!AECoerceDesc(&objTokenDesc, typeMyText, &newDesc)) {
  1287.             GetRawDataFromDescriptor(&newDesc, (Ptr)&theTextToken, sizeof(theTextToken), &tokenSize);
  1288.  
  1289.             myErr         = AEDisposeDesc(&newDesc);
  1290.             theDocument = DPtrFromWindowPtr(theTextToken.tokenWindow);
  1291.             theHTE        = theDocument->theText;
  1292.  
  1293.             TESetSelect(
  1294.                 theTextToken.tokenOffset-1,
  1295.                 theTextToken.tokenOffset+theTextToken.tokenLength-1,
  1296.                 theHTE);
  1297.                 
  1298.             if (theDocument->kind == kDocumentWindow) {
  1299.                 DoTEDeleteSectionRecalc(theDocument);
  1300.             } else if (!theDocument->u.cons.selected || (*theHTE)->selStart < theDocument->u.cons.fence) {
  1301.                 SysBeep(1);
  1302.                 
  1303.                 return errAEEventNotHandled;
  1304.             }
  1305.             
  1306.             TEDelete(theHTE);
  1307.             myErr = GetTextFromDescIntoTEHandle(dataDesc, theHTE);
  1308.             EnforceMemory(theDocument, theHTE);
  1309.                 
  1310.             theDocument->dirty = true;
  1311.         }
  1312.  
  1313.     ignoreErr = AEDisposeDesc(&objTokenDesc);
  1314.  
  1315.     return myErr;
  1316. }    /* HandleSetData */
  1317.  
  1318. /*
  1319.     A few convenient FORWARDS...
  1320. */
  1321.  
  1322. pascal OSErr MakeWindowObj(WindowPtr theWindow, AEDesc *dMyDoc);
  1323.  
  1324. /*
  1325.     Back to real code
  1326. */
  1327. pascal OSErr MakeSelTextObj(WindowPtr theWindow, TEHandle theTextEditHandle, AEDesc *selTextObj)
  1328. /*
  1329.     This is a hack to get the AppleScript Alpha to work...
  1330. */
  1331. {
  1332.     OSErr    myErr;
  1333.     OSErr    ignoreErr;
  1334.     AEDesc   dNull;
  1335.     AEDesc   dMyDoc;
  1336.     AEDesc   startOfs;
  1337.     AEDesc   endOfs;
  1338.     AEDesc   startObj;
  1339.     AEDesc   endObj;
  1340.     AEDesc   rangeDesc;
  1341.     long     startChar;
  1342.     long     endChar;
  1343.     Boolean  spotFlag;
  1344.  
  1345.     myErr = noErr;
  1346.  
  1347.     if (theWindow==nil)
  1348.         return noErr;
  1349.  
  1350.     selTextObj->dataHandle = nil;
  1351.     dMyDoc.dataHandle      = nil;
  1352.     startObj.dataHandle    = nil;
  1353.     endObj.dataHandle      = nil;
  1354.  
  1355.     /*
  1356.         make the window object
  1357.     */
  1358.  
  1359.     if (myErr = MakeWindowObj(theWindow, &dMyDoc))
  1360.         return myErr;
  1361.  
  1362.     /* get the start and end of selection */
  1363.  
  1364.     startChar = (*theTextEditHandle)->selStart+1;    /* start counting obj's from 1, not 0 */
  1365.     endChar   = (*theTextEditHandle)->selEnd;
  1366.     spotFlag  = ((*theTextEditHandle)->selStart == (*theTextEditHandle)->selEnd);
  1367.  
  1368.     if (myErr = CreateOffsetDescriptor(startChar, &startOfs))
  1369.         return myErr;
  1370.  
  1371.     if (spotFlag)
  1372.         myErr = CreateObjSpecifier(cSpot, &dMyDoc, formAbsolutePosition, &startOfs, true, selTextObj);
  1373.     else {
  1374.         /* not a spot - must represent as range */
  1375.         /* make obj for start char */
  1376.  
  1377.         myErr = AECreateDesc(typeNull, nil , 0, &dNull);
  1378.  
  1379.         myErr = CreateObjSpecifier(cChar, &dNull, formAbsolutePosition, &startOfs, false, &startObj);
  1380.  
  1381.         if (myErr==noErr)
  1382.             myErr = CreateOffsetDescriptor(endChar, &endOfs);
  1383.  
  1384.         if (myErr==noErr)
  1385.             myErr = CreateObjSpecifier(cChar, &dNull, formAbsolutePosition, &endOfs, false, &endObj);
  1386.  
  1387.         if (myErr==noErr)
  1388.             myErr = CreateRangeDescriptor(&startObj, &endObj, false, &rangeDesc);
  1389.  
  1390.         if (myErr==noErr)
  1391.             myErr = CreateObjSpecifier(cChar, &dMyDoc, formRange, &rangeDesc, true, selTextObj);
  1392.  
  1393.         if (startObj.dataHandle)
  1394.           ignoreErr = AEDisposeDesc(&startObj);
  1395.  
  1396.         if (startOfs.dataHandle)
  1397.           ignoreErr = AEDisposeDesc(&startOfs);
  1398.  
  1399.         if (endObj.dataHandle)
  1400.           ignoreErr = AEDisposeDesc(&endObj);
  1401.  
  1402.         if (endOfs.dataHandle)
  1403.           ignoreErr = AEDisposeDesc(&endOfs);
  1404.     }
  1405.  
  1406.     return myErr;
  1407. }    /* MakeSelTextObj */
  1408.  
  1409. /* -----------------------------------------------------------------------
  1410.         Name:             DoSetData
  1411.         Purpose:        Handles the SetData Apple Event, extracting the direct
  1412.                                 object (which says what to set) and the data (what to set
  1413.                                 it to).
  1414.      -----------------------------------------------------------------------**/
  1415.  
  1416. pascal OSErr DoSetData(const AppleEvent *theAppleEvent, AppleEvent *reply, long handlerRefCon)
  1417. {
  1418. #pragma unused (reply, handlerRefCon)
  1419.  
  1420.     OSErr  myErr;
  1421.     OSErr  ignoreErr;
  1422.     AEDesc myDirObj;
  1423.     AEDesc myDataDesc;
  1424.  
  1425.     myDataDesc.dataHandle = nil;
  1426.     myDirObj.dataHandle   = nil;
  1427.  
  1428.     /* pick up the direct object, which is the object whose data is to be set */
  1429.  
  1430.     myErr = AEGetParamDesc(theAppleEvent, keyDirectObject, typeWildCard, &myDirObj);
  1431.  
  1432.     /* now the data to set it to - typeWildCard means get as is*/
  1433.     if (myErr == noErr)
  1434.         myErr = AEGetParamDesc(theAppleEvent, keyAEData, typeWildCard, &myDataDesc);
  1435.  
  1436.     /* missing any parameters? */
  1437.     if (myErr == noErr)
  1438.         myErr = GotRequiredParams(theAppleEvent);
  1439.  
  1440.     /* set the data */
  1441.     if (myErr == noErr)
  1442.         myErr = HandleSetData(&myDirObj, &myDataDesc);
  1443.  
  1444.     if (myDataDesc.dataHandle)
  1445.         ignoreErr = AEDisposeDesc(&myDataDesc);
  1446.  
  1447.     if (myDirObj.dataHandle)
  1448.         ignoreErr = AEDisposeDesc(&myDirObj);
  1449.  
  1450.     return myErr;
  1451. }    /* DoSetData */
  1452.  
  1453. pascal OSErr BuildStyledTextDesc(TEHandle theHTE, short start, short howLong, AEDesc *resultDesc)
  1454. {
  1455.     AEDesc       listDesc;
  1456.     OSErr        myErr;
  1457.     OSErr        ignoreErr;
  1458.  
  1459.     listDesc.dataHandle = nil;
  1460.  
  1461.     TESetSelect(start-1, start+howLong-2, theHTE);
  1462.  
  1463.     myErr = AECreateList(nil, 0, true,  &listDesc);
  1464.  
  1465.     HLock((Handle)(**theHTE).hText);
  1466.  
  1467.     if (myErr==noErr)
  1468.         myErr = AEPutKeyPtr(&listDesc,
  1469.                                   keyAEText,
  1470.                                                 typeChar,
  1471.                                                 (Ptr)&(*(**theHTE).hText)[start-1],
  1472.                                                 howLong);
  1473.  
  1474.     HUnlock((Handle)(**theHTE).hText);
  1475.  
  1476.     myErr = AEPutKeyPtr(&listDesc, keyAEStyles, typeScrapStyles, (Ptr)nil, 0);
  1477.  
  1478.     if (myErr==noErr)
  1479.         myErr = AECoerceDesc(&listDesc, typeStyledText, resultDesc); // should be typeIntlText
  1480.  
  1481.     if (listDesc.dataHandle)
  1482.         ignoreErr = AEDisposeDesc(&listDesc);
  1483.  
  1484.     return myErr;
  1485. }
  1486.  
  1487. /* -----------------------------------------------------------------------
  1488.         Name:             GetTextProperty
  1489.         Purpose:        Fills dataDesc with the requested text property.
  1490.      -----------------------------------------------------------------------**/
  1491.  
  1492. pascal OSErr GetTextProperty(const AEDesc *theTokenDesc, AEDesc *dataDesc)
  1493. {
  1494.       DPtr          theDocument;
  1495.     TEHandle      theHTE;
  1496.     short         theSize;
  1497.     GrafPtr       oldPort;
  1498.     textPropToken theTextPropToken;
  1499.     OSErr         myErr;
  1500.     Size          tokenSize;
  1501.     AEDesc        newDesc;
  1502.  
  1503.       if (myErr = AECoerceDesc(theTokenDesc, typeMyTextProp, &newDesc))
  1504.         return myErr;
  1505.  
  1506.     GetRawDataFromDescriptor(&newDesc, (Ptr)&theTextPropToken, sizeof(theTextPropToken), &tokenSize);
  1507.     myErr= AEDisposeDesc(&newDesc);
  1508.  
  1509.     /*
  1510.         For each property we build a descriptor to be returned as the reply.
  1511.     */
  1512.  
  1513.     theDocument = DPtrFromWindowPtr(theTextPropToken.propertyTextToken.tokenWindow);
  1514.     theHTE         = theDocument->theText;
  1515.  
  1516.     if (theTextPropToken.propertyProperty == pText)
  1517.         myErr =
  1518.             BuildStyledTextDesc(
  1519.                 theHTE,
  1520.                 theTextPropToken.propertyTextToken.tokenOffset,
  1521.                 theTextPropToken.propertyTextToken.tokenLength,
  1522.                 dataDesc);
  1523.     else if (theTextPropToken.propertyProperty == pStringWidth)  {
  1524.         GetPort(&oldPort);
  1525.         SetPort(theTextPropToken.propertyTextToken.tokenWindow);
  1526.  
  1527.         HLock((Handle)(*theHTE)->hText);
  1528.         theSize =
  1529.             TextWidth(
  1530.                 &(*theHTE)->hText,
  1531.                 theTextPropToken.propertyTextToken.tokenOffset-1,
  1532.                 theTextPropToken.propertyTextToken.tokenLength);
  1533.         HUnlock((Handle)(*theHTE)->hText);
  1534.  
  1535.         SetPort(oldPort);
  1536.         return CreateOffsetDescriptor(theSize, dataDesc);
  1537.     }  else
  1538.         myErr = errAEEventNotHandled;
  1539.  
  1540.     return myErr;
  1541. } /*GetTextProperty*/
  1542.  
  1543. /* -----------------------------------------------------------------------
  1544.         Name:             GetWindowProperty
  1545.         Purpose:        Fills dataDesc with the requested window property.
  1546.      -----------------------------------------------------------------------**/
  1547. typedef Rect **RectHandle;
  1548.  
  1549. pascal OSErr GetWindowProperty(const AEDesc *theWPTokenObj, AEDesc *dataDesc)
  1550. {
  1551.      OSErr           theErr;
  1552.     Str255          theName;
  1553.     Boolean         theBoolean;
  1554.     Rect            theRect;
  1555.     Point           thePoint;
  1556.     Rect            winRect;
  1557.     Rect            userRect;
  1558.     short           theIndex;
  1559.     DPtr            theDocument;
  1560.     TEHandle        theHTE;
  1561.     windowPropToken theWindowPropToken;
  1562.     AEDesc          newDesc;
  1563.     Size            tokenSize;
  1564.  
  1565.       if (theErr = AECoerceDesc(theWPTokenObj,typeMyWindowProp, &newDesc))
  1566.           return theErr;
  1567.  
  1568.     GetRawDataFromDescriptor(
  1569.         &newDesc,
  1570.         (Ptr)&theWindowPropToken,
  1571.         sizeof(theWindowPropToken),
  1572.         &tokenSize);
  1573.  
  1574.     theErr = AEDisposeDesc(&newDesc);
  1575.  
  1576.     theErr = kAEGenericErr;
  1577.  
  1578.     if (theWindowPropToken.tokenProperty == pName)  {
  1579.         GetWTitle(theWindowPropToken.tokenWindowToken, theName);
  1580.         theErr = AECreateDesc(typeChar, (Ptr)&theName[1], theName[0], dataDesc);
  1581.     }
  1582.  
  1583.     if (theWindowPropToken.tokenProperty == pText) {
  1584.         theDocument = DPtrFromWindowPtr(theWindowPropToken.tokenWindowToken);
  1585.         theHTE      = theDocument->theText;
  1586.  
  1587.         theErr = BuildStyledTextDesc(theHTE, 1, (**theHTE).teLength, dataDesc);
  1588.     }
  1589.  
  1590.     if (theWindowPropToken.tokenProperty == pBounds) {
  1591.         SetPort(theWindowPropToken.tokenWindowToken);
  1592.  
  1593.         theRect = (*((WindowPeek)theWindowPropToken.tokenWindowToken)->strucRgn)->rgnBBox;
  1594.  
  1595.         theErr  = AECreateDesc(typeQDRectangle, (Ptr)&theRect, sizeof(theRect), dataDesc);
  1596.     }
  1597.  
  1598.     if (theWindowPropToken.tokenProperty == pPosition) {
  1599.         thePoint.v = (*((WindowPeek)theWindowPropToken.tokenWindowToken)->strucRgn)->rgnBBox.top;
  1600.         thePoint.h = (*((WindowPeek)theWindowPropToken.tokenWindowToken)->strucRgn)->rgnBBox.left;
  1601.  
  1602.         theErr   = AECreateDesc(typeQDPoint, (Ptr)&thePoint, sizeof(thePoint), dataDesc);
  1603.     }
  1604.  
  1605.     if (theWindowPropToken.tokenProperty == pVisible) {
  1606.         theBoolean = ((WindowPeek)theWindowPropToken.tokenWindowToken)->visible;
  1607.  
  1608.         theErr  = AECreateDesc(typeBoolean, (Ptr)&theBoolean, sizeof(theBoolean), dataDesc);
  1609.     }
  1610.  
  1611.     if (theWindowPropToken.tokenProperty == pIsModal) {
  1612.         theBoolean = false;
  1613.  
  1614.         theErr  = AECreateDesc(typeBoolean, (Ptr)&theBoolean, sizeof(theBoolean), dataDesc);
  1615.     }
  1616.  
  1617.     if (theWindowPropToken.tokenProperty == pShowBorders) {
  1618.         theDocument  = DPtrFromWindowPtr(theWindowPropToken.tokenWindowToken);
  1619.         theBoolean   = (theDocument->kind == kDocumentWindow) ? theDocument->u.reg.showBorders : false;
  1620.  
  1621.         theErr  = AECreateDesc(typeBoolean, (Ptr)&theBoolean, sizeof(theBoolean), dataDesc);
  1622.     }
  1623.  
  1624.     if (theWindowPropToken.tokenProperty == pIsZoomed)  {
  1625.         if (((WindowPeek)theWindowPropToken.tokenWindowToken)->spareFlag) {
  1626.             SetPort(theWindowPropToken.tokenWindowToken);
  1627.  
  1628.             userRect = **((RectHandle)((WindowPeek)qd.thePort)->dataHandle);
  1629.             winRect  = qd.thePort->portRect;
  1630.             LocalToGlobal((Point *)&winRect.top);
  1631.             LocalToGlobal((Point *)&winRect.bottom);
  1632.  
  1633.             theBoolean = !EqualRect(&userRect, &winRect);
  1634.         } else
  1635.             theBoolean = false;
  1636.  
  1637.         theErr  = AECreateDesc(typeBoolean, (Ptr)&theBoolean, sizeof(theBoolean), dataDesc);
  1638.     }
  1639.  
  1640.     if ((theWindowPropToken.tokenProperty == pIsResizable) ||
  1641.          (theWindowPropToken.tokenProperty == pHasTitleBar) ||
  1642.          (theWindowPropToken.tokenProperty == pIsZoomable)
  1643.     ) {
  1644.         theBoolean = theDocument->kind != kWorksheetWindow;
  1645.  
  1646.         theErr  = AECreateDesc(typeBoolean, (Ptr)&theBoolean, sizeof(theBoolean), dataDesc);
  1647.     }
  1648.  
  1649.     if (theWindowPropToken.tokenProperty == pHasCloseBox) {
  1650.         theBoolean = true;
  1651.  
  1652.         theErr  = AECreateDesc(typeBoolean, (Ptr)&theBoolean, sizeof(theBoolean), dataDesc);
  1653.     }
  1654.  
  1655.     if (theWindowPropToken.tokenProperty == pIsFloating) {
  1656.         theBoolean = false;
  1657.  
  1658.         theErr  = AECreateDesc(typeBoolean, (Ptr)&theBoolean, sizeof(theBoolean), dataDesc);
  1659.     }
  1660.  
  1661.     if (theWindowPropToken.tokenProperty == pIsModified) {
  1662.         theDocument = DPtrFromWindowPtr(theWindowPropToken.tokenWindowToken);
  1663.  
  1664.         theBoolean  = (theDocument->kind == kDocumentWindow) ? theDocument->dirty : true;
  1665.  
  1666.         theErr  = AECreateDesc(typeBoolean, (Ptr)&theBoolean, sizeof(theBoolean), dataDesc);
  1667.     }
  1668.  
  1669.     if (theWindowPropToken.tokenProperty == pIndex) {
  1670.         theIndex = 0;
  1671.         if (theWindowPropToken.tokenWindowToken)
  1672.             do
  1673.                 theIndex++;
  1674.             while (theWindowPropToken.tokenWindowToken != GetWindowPtrOfNthWindow(theIndex));
  1675.  
  1676.         theErr  = AECreateDesc(typeShortInteger, (Ptr)theIndex, sizeof(theIndex), dataDesc);
  1677.     }
  1678.  
  1679.     if (theWindowPropToken.tokenProperty == pPageSetup) {
  1680.         theDocument = DPtrFromWindowPtr(theWindowPropToken.tokenWindowToken);
  1681.  
  1682.         HLock((Handle)theDocument->thePrintSetup);
  1683.  
  1684.         theErr  = AECreateDesc(typeTPrint, (Ptr)*(theDocument->thePrintSetup), sizeof(TPrint), dataDesc);
  1685.  
  1686.         HUnlock((Handle)theDocument->thePrintSetup);
  1687.     }
  1688.  
  1689.     if (theWindowPropToken.tokenProperty == pSelection) {
  1690.         theDocument = DPtrFromWindowPtr(theWindowPropToken.tokenWindowToken);
  1691.  
  1692.         theErr  = MakeSelTextObj(theWindowPropToken.tokenWindowToken, theDocument->theText, dataDesc);
  1693.     }
  1694.     
  1695.     if (theWindowPropToken.tokenProperty == pFont) {
  1696.         theDocument = DPtrFromWindowPtr(theWindowPropToken.tokenWindowToken);
  1697.         
  1698.         GetFontName((*theDocument->theText)->txFont, theName);
  1699.  
  1700.         theErr = AECreateDesc(typeChar, (Ptr)&theName[1], theName[0], dataDesc);
  1701.     } 
  1702.     
  1703.     if (theWindowPropToken.tokenProperty == pPointSize) {
  1704.         theDocument = DPtrFromWindowPtr(theWindowPropToken.tokenWindowToken);
  1705.         theErr =CreateOffsetDescriptor((*theDocument->theText)->txSize, dataDesc);
  1706.     }
  1707.         
  1708.     if (theWindowPropToken.tokenProperty == pScriptTag) {
  1709.         theDocument = DPtrFromWindowPtr(theWindowPropToken.tokenWindowToken);
  1710.         theErr = CreateOffsetDescriptor(FontToScript((*theDocument->theText)->txFont), dataDesc);
  1711.     }
  1712.  
  1713.     return theErr;
  1714. } /* GetWindowProperty */
  1715.  
  1716. /** -----------------------------------------------------------------------
  1717.         Name:             GetApplicationProperty
  1718.         Purpose:        Fills dataDesc with the requested application property.
  1719.      -----------------------------------------------------------------------**/
  1720.  
  1721. pascal OSErr GetApplicationProperty(const AEDesc *theObjToken, AEDesc *dataDesc)
  1722. {
  1723.       OSErr         theErr;
  1724.     Str255        theName;
  1725.     Boolean       isFront;
  1726.     applPropToken theApplPropToken;
  1727.     AEDesc        newDesc;
  1728.     Size          tokenSize;
  1729.  
  1730.     if (theErr = AECoerceDesc(theObjToken, typeMyApplProp, &newDesc))
  1731.         return theErr;
  1732.  
  1733.     GetRawDataFromDescriptor(&newDesc, (Ptr)&theApplPropToken, sizeof(theApplPropToken), &tokenSize);
  1734.  
  1735.     theErr = AEDisposeDesc(&newDesc);
  1736.     theErr = kAEGenericErr;
  1737.  
  1738.     if (theApplPropToken.tokenApplProperty == pName) {
  1739.         PLstrcpy((StringPtr)theName, "\pMacPerl");
  1740.         theErr  = AECreateDesc(typeChar, (Ptr)&theName[1], theName[0], dataDesc);
  1741.     }
  1742.  
  1743.     if (theApplPropToken.tokenApplProperty == pVersion)  {
  1744.         PLstrcpy((StringPtr)theName, "\p4.1.0");
  1745.         theErr  = AECreateDesc(typeChar, (Ptr)&theName[1], theName[0], dataDesc);
  1746.     }
  1747.  
  1748.     if (theApplPropToken.tokenApplProperty == pIsFrontProcess) {
  1749.         isFront = !gInBackground;
  1750.         theErr  = AECreateDesc(typeBoolean, (Ptr)&isFront, sizeof(isFront), dataDesc);
  1751.     }
  1752.  
  1753.     return theErr;
  1754. } /* GetApplicationProperty */
  1755.  
  1756. /** -----------------------------------------------------------------------
  1757.         Name:             GetMenuProperty
  1758.         Purpose:        Fills dataDesc with the requested menu property.
  1759.      -----------------------------------------------------------------------**/
  1760.  
  1761. pascal OSErr GetMenuProperty(const AEDesc *theObjToken, AEDesc *dataDesc)
  1762. {
  1763.       OSErr         theErr;
  1764.     Str255        theName;
  1765.     MenuPropToken theMenuPropToken;
  1766.     AEDesc        newDesc;
  1767.     Size          tokenSize;
  1768.  
  1769.     if (theErr = AECoerceDesc(theObjToken, typeMyMenuProp, &newDesc))
  1770.         return theErr;
  1771.  
  1772.     GetRawDataFromDescriptor(&newDesc, (Ptr)&theMenuPropToken, sizeof(theMenuPropToken), &tokenSize);
  1773.  
  1774.     theErr = AEDisposeDesc(&newDesc);
  1775.     theErr = kAEGenericErr;
  1776.  
  1777.     if (theMenuPropToken.theMenuProp == pName)  {
  1778.           PLstrcpy(theName, (**theMenuPropToken.theMenuToken.theTokenMenu).menuData);
  1779.         theErr  = AECreateDesc(typeChar, (Ptr)&theName[1], theName[0], dataDesc);
  1780.     }
  1781.  
  1782.     if (theMenuPropToken.theMenuProp == pMenuID) {
  1783.         theErr  =
  1784.             AECreateDesc(
  1785.                 typeShortInteger,
  1786.                 (Ptr)&theMenuPropToken.theMenuToken.theTokenID,
  1787.                 sizeof(theMenuPropToken.theMenuToken.theTokenID),
  1788.                 dataDesc);
  1789.     }
  1790.  
  1791.     return theErr;
  1792. } /* GetMenuProperty */
  1793.  
  1794. /** -----------------------------------------------------------------------
  1795.         Name:         GetMenuItemProperty
  1796.         Purpose:        Fills dataDesc with the requested menu property.
  1797.      -----------------------------------------------------------------------**/
  1798.  
  1799. pascal OSErr GetMenuItemProperty(const AEDesc *theObjToken, AEDesc *dataDesc)
  1800. {
  1801.       OSErr             theErr;
  1802.     Str255            theName;
  1803.     MenuItemPropToken theMenuItemPropToken;
  1804.     AEDesc            newDesc;
  1805.     Size              tokenSize;
  1806.  
  1807.     if (theErr = AECoerceDesc(theObjToken, typeMyItemProp, &newDesc))
  1808.         return theErr;
  1809.  
  1810.     GetRawDataFromDescriptor(&newDesc, (Ptr)&theMenuItemPropToken, sizeof(theMenuItemPropToken), &tokenSize);
  1811.  
  1812.     theErr = AEDisposeDesc(&newDesc);
  1813.     theErr = kAEGenericErr;
  1814.  
  1815.     if (theMenuItemPropToken.theItemProp == pName) {
  1816.           GetItem(
  1817.             theMenuItemPropToken.theItemToken.theMenuToken.theTokenMenu,
  1818.             theMenuItemPropToken.theItemToken.theTokenItem,
  1819.             theName);
  1820.         theErr  = AECreateDesc(typeChar, (Ptr)&theName[1], theName[0], dataDesc);
  1821.     }
  1822.  
  1823.     if (theMenuItemPropToken.theItemProp == pItemNumber) {
  1824.         theErr  =
  1825.             AECreateDesc(
  1826.                 typeShortInteger,
  1827.                 (Ptr)&theMenuItemPropToken.theItemToken.theTokenItem,
  1828.                 sizeof(theMenuItemPropToken.theItemToken.theTokenItem),
  1829.                 dataDesc);
  1830.     }
  1831.  
  1832.     return theErr;
  1833. } /* GetMenuItemProperty */
  1834.  
  1835. /** -----------------------------------------------------------------------
  1836.         Name:         HandleGetData
  1837.         Purpose:        Coerces theObj into a token which we understand and
  1838.                         extracts the data requested in the token and puts it
  1839.                         into dataDesc.
  1840.      -----------------------------------------------------------------------**/
  1841.  
  1842. typedef char chars[32001];
  1843. typedef chars **charsHandle;
  1844.  
  1845. pascal OSErr HandleGetData(AEDesc *theObj, DescType whatType, AEDesc *dataDesc)
  1846. {
  1847. #pragma unused (whatType)
  1848.  
  1849.       OSErr           myErr;
  1850.     AEDesc          newDesc;
  1851.     TextToken       theTextToken;
  1852.     Size            tokenSize;
  1853.     DPtr            theDoc;
  1854.     AEDesc          objTokenDesc;
  1855.  
  1856.     myErr = errAEWrongDataType;
  1857.  
  1858.     /*
  1859.         Coerce theObj into a token which we can use -
  1860.              set the property for that token
  1861.     */
  1862.  
  1863.  
  1864.     if (myErr = AEResolve(theObj, kAEIDoMinimum, &objTokenDesc))
  1865.         return myErr;
  1866.  
  1867.     if (objTokenDesc.descriptorType == typeMyApplProp)
  1868.         myErr = GetApplicationProperty(&objTokenDesc, dataDesc);
  1869.     else if (objTokenDesc.descriptorType == typeMyMenuProp)
  1870.         myErr = GetMenuProperty(&objTokenDesc, dataDesc);
  1871.     else if (objTokenDesc.descriptorType == typeMyItemProp)
  1872.         myErr = GetMenuItemProperty(&objTokenDesc, dataDesc);
  1873.     else if (objTokenDesc.descriptorType == typeMyTextProp)
  1874.         myErr = GetTextProperty(&objTokenDesc, dataDesc);
  1875.     else if (objTokenDesc.descriptorType == typeMyWindowProp)
  1876.         myErr = GetWindowProperty(&objTokenDesc, dataDesc);
  1877.     else if (objTokenDesc.descriptorType == typeMyText)
  1878.         if (!AECoerceDesc(&objTokenDesc, typeMyText, &newDesc)) {
  1879.             GetRawDataFromDescriptor(
  1880.                 &newDesc,
  1881.                 (Ptr)&theTextToken,
  1882.                 sizeof(theTextToken),
  1883.                 &tokenSize);
  1884.  
  1885.             myErr     = AEDisposeDesc(&newDesc);
  1886.  
  1887.             theDoc    = DPtrFromWindowPtr(theTextToken.tokenWindow);
  1888.  
  1889.             myErr     =
  1890.                 BuildStyledTextDesc(
  1891.                     theDoc->theText,
  1892.                     theTextToken.tokenOffset,
  1893.                     theTextToken.tokenLength,
  1894.                     dataDesc);
  1895.         }
  1896.  
  1897.     return myErr;
  1898. }    /* HandleGetData */
  1899.  
  1900. /** -----------------------------------------------------------------------
  1901.         Name:         DoGetData
  1902.         Purpose:        Handles the GetData AppleEvent.
  1903.      -----------------------------------------------------------------------**/
  1904.  
  1905. pascal OSErr DoGetData(
  1906.     const AppleEvent *theAppleEvent,
  1907.     AppleEvent *reply,
  1908.     long handlerRefCon)
  1909. {
  1910. #pragma unused (handlerRefCon)
  1911.  
  1912.     OSErr    myErr;
  1913.     OSErr    tempErr;
  1914.     AEDesc   myDirObj;
  1915.     AEDesc   myDataDesc;
  1916.     Size     actualSize;
  1917.     DescType returnedType;
  1918.     DescType reqType;
  1919.  
  1920.     myDataDesc.dataHandle = nil;
  1921.     myDirObj.dataHandle   = nil;
  1922.  
  1923.     /*
  1924.         extract the direct object, which is the object whose data is to be returned
  1925.     */
  1926.  
  1927.     myErr  = AEGetParamDesc(theAppleEvent, keyDirectObject, typeWildCard, &myDirObj);
  1928.  
  1929.     /*
  1930.         now the get the type of data wanted - optional
  1931.     */
  1932.  
  1933.     tempErr =
  1934.         AEGetParamPtr(
  1935.             theAppleEvent,
  1936.             keyAERequestedType,
  1937.             typeType,
  1938.             &returnedType,
  1939.             (Ptr)&reqType,
  1940.             sizeof(reqType),
  1941.             &actualSize);
  1942.  
  1943.     if (tempErr!=noErr)
  1944.         reqType = typeChar;
  1945.  
  1946.     if (myErr == noErr)
  1947.         myErr = GotRequiredParams(theAppleEvent);
  1948.  
  1949.     /* get the data */
  1950.     if (myErr == noErr)
  1951.         myErr = HandleGetData(&myDirObj, reqType, &myDataDesc);
  1952.  
  1953.     /* if they wanted a reply, attach it now */
  1954.     if (myErr==noErr)
  1955.         if (reply->descriptorType != typeNull)
  1956.             myErr = AEPutParamDesc(reply, keyDirectObject, &myDataDesc);
  1957.  
  1958.      if (myDataDesc.dataHandle)
  1959.           tempErr = AEDisposeDesc(&myDataDesc);
  1960.  
  1961.      if (myDirObj.dataHandle)
  1962.           tempErr = AEDisposeDesc(&myDirObj);
  1963.  
  1964.     return myErr;
  1965. }    /* DoGetData */
  1966.  
  1967.  
  1968. /** -----------------------------------------------------------------------
  1969.         Name:         DoGetDataSize
  1970.         Purpose:        Handles the GetDataSize AppleEvent.
  1971.      -----------------------------------------------------------------------**/
  1972.  
  1973. pascal OSErr DoGetDataSize(
  1974.     const AppleEvent *theAppleEvent,
  1975.     AppleEvent *reply,
  1976.     long       handlerRefCon)
  1977. {
  1978. #pragma unused (handlerRefCon)
  1979.  
  1980.     OSErr     myErr;
  1981.     OSErr     tempErr;
  1982.     AEDesc    myDirObj;
  1983.     AEDesc    myDataDesc;
  1984.     Size      actualSize;
  1985.     DescType  returnedType;
  1986.     DescType  reqType;
  1987.     long      dataSize;
  1988.  
  1989.     myDataDesc.dataHandle = nil;
  1990.     myDirObj.dataHandle = nil;
  1991.  
  1992.     /* pick up the direct object, which is the object whose data is to be sized */
  1993.  
  1994.     myErr  = AEGetParamDesc(theAppleEvent, keyDirectObject, typeWildCard, &myDirObj);
  1995.  
  1996.     /* now the get the type wanted - optional*/
  1997.  
  1998.     tempErr =
  1999.         AEGetParamPtr(
  2000.             theAppleEvent,
  2001.             keyAERequestedType,
  2002.             typeType,
  2003.             &returnedType,
  2004.             (Ptr)&reqType,
  2005.             sizeof(reqType),
  2006.             &actualSize);
  2007.  
  2008.     if (tempErr!=noErr)
  2009.         reqType = typeChar;
  2010.  
  2011.     if (myErr == noErr)
  2012.         myErr = GotRequiredParams(theAppleEvent);
  2013.  
  2014.     /* get the data */
  2015.     if (myErr == noErr)
  2016.         myErr = HandleGetData(&myDirObj, reqType, &myDataDesc);
  2017.  
  2018.     /* evaluate size of data and discard, create desc for size */
  2019.     if (myErr == noErr)
  2020.         if (myDataDesc.dataHandle) {
  2021.             dataSize = GetHandleSize((Handle)myDataDesc.dataHandle);
  2022.             DisposHandle((Handle)myDataDesc.dataHandle);
  2023.             myErr  = AECreateDesc(typeLongInteger, (Ptr)&dataSize, sizeof(dataSize), &myDataDesc);
  2024.         }
  2025.  
  2026.  
  2027.     /* if they wanted a reply, attach it now */
  2028.  
  2029.     if (myErr==noErr)
  2030.         if (reply->descriptorType != typeNull)
  2031.             myErr = AEPutParamDesc(reply, keyDirectObject, &myDataDesc);
  2032.  
  2033.     /* discard our copy */
  2034.  
  2035.     if (myDataDesc.dataHandle)
  2036.         tempErr = AEDisposeDesc(&myDataDesc);
  2037.  
  2038.     if (myDirObj.dataHandle)
  2039.         tempErr = AEDisposeDesc(&myDirObj);
  2040.  
  2041.     return myErr;
  2042. }    /* DoGetDataSize */
  2043.  
  2044. /** -----------------------------------------------------------------------
  2045.         Name:         DoNewElement
  2046.         Purpose:        Handles the NewElement AppleEvent. Only Creates windows for
  2047.                     now.
  2048.      -----------------------------------------------------------------------**/
  2049.  
  2050. pascal OSErr DoNewElement(
  2051.     const AppleEvent *theAppleEvent,
  2052.     AppleEvent *reply,
  2053.     long       handlerRefCon)
  2054. {
  2055. #pragma unused (handlerRefCon)
  2056.  
  2057.     OSErr       myErr;
  2058.     OSErr       ignoreErr;
  2059.     DescType      returnedType;
  2060.     DescType      newElemClass;
  2061.     Size        actSize;
  2062.     AEDesc        wndwObjSpec;
  2063.     DPtr        theDoc;
  2064.  
  2065.     wndwObjSpec.dataHandle = nil;
  2066.  
  2067.     myErr =
  2068.         AEGetParamPtr(
  2069.             theAppleEvent,
  2070.             keyAEObjectClass,
  2071.             typeType,
  2072.             &returnedType,
  2073.             (Ptr)&newElemClass,
  2074.             sizeof(newElemClass),
  2075.             &actSize);
  2076.  
  2077.   /* check for missing required parameters */
  2078.  
  2079.   if (myErr == noErr)
  2080.         myErr = GotRequiredParams(theAppleEvent);
  2081.  
  2082.   /* got all required params */
  2083.  
  2084.   /* let's make sure container is the null desc */
  2085.   /* and they want a window */
  2086.  
  2087.   if (newElemClass != cWindow)
  2088.     myErr = errAEWrongDataType;
  2089.  
  2090.   /* let's create a new window */
  2091.  
  2092.     if (myErr == noErr)
  2093.         theDoc = NewDocument(false, kDocumentWindow);
  2094.  
  2095.     if (myErr==noErr)
  2096.         if (theDoc == nil)
  2097.             myErr = -1700;
  2098.         else {
  2099.             DoShowWindow(theDoc->theWindow);
  2100.             theDoc->dirty = false;
  2101.  
  2102.             myErr = MakeWindowObj(theDoc->theWindow, &wndwObjSpec);
  2103.         }
  2104.  
  2105.     if (myErr == noErr)
  2106.         if (reply->descriptorType != typeNull)
  2107.              myErr = AEPutParamDesc(reply, keyDirectObject, &wndwObjSpec);
  2108.  
  2109.     if (wndwObjSpec.dataHandle)
  2110.         ignoreErr = AEDisposeDesc(&wndwObjSpec);
  2111.  
  2112.       return myErr;
  2113. }    /* DoNewElement */
  2114.  
  2115. /** -----------------------------------------------------------------------
  2116.         Name:         DoIsThereA
  2117.         Purpose:        Handles the IsThereA AppleEvent.
  2118.      -----------------------------------------------------------------------**/
  2119.  
  2120. pascal OSErr DoIsThereA(
  2121.     const AppleEvent *theAppleEvent,
  2122.     AppleEvent       *reply,
  2123.     long             handlerRefCon)
  2124. /*
  2125.     Support check of Windows at first
  2126.  
  2127.     What we do :
  2128.         Get Direct Object
  2129.         Check have all required params
  2130.         Coerce into things we support
  2131.         if we get something back
  2132.             check to see it exists and set reply
  2133.         clean up
  2134.         return
  2135. */
  2136. {
  2137. #pragma unused (handlerRefCon)
  2138.  
  2139.     OSErr         myErr;
  2140.     OSErr         ignoreErr;
  2141.     AEDesc        myDirObject;
  2142.     AEDesc        windDesc;
  2143.     AEDesc        dataDesc;
  2144.     WindowToken   theWindowToken;
  2145.     Size          tokenSize;
  2146.     Boolean       exists;
  2147.  
  2148.     myDirObject.dataHandle = nil;
  2149.     windDesc.dataHandle    = nil;
  2150.     dataDesc.dataHandle    = nil;
  2151.  
  2152.     myErr = AEGetParamDesc(theAppleEvent, keyDirectObject, typeWildCard, &myDirObject);
  2153.  
  2154.     /* check for missing required parameters */
  2155.  
  2156.     if (myErr == noErr)
  2157.         myErr = GotRequiredParams(theAppleEvent);
  2158.  
  2159.     /* got all required params */
  2160.  
  2161.     /* let's make sure they want to check for a window */
  2162.  
  2163.     exists = false;
  2164.  
  2165.     if (myErr == noErr)
  2166.         if (AECoerceDesc(&myDirObject, typeMyWndw, &windDesc)==noErr)
  2167.             if (windDesc.descriptorType!=typeNull) {
  2168.                 GetRawDataFromDescriptor(
  2169.                     &windDesc,
  2170.                     (Ptr)&theWindowToken,
  2171.                     sizeof(theWindowToken),
  2172.                     &tokenSize);
  2173.  
  2174.                 exists = (theWindowToken != nil);
  2175.             }
  2176.  
  2177.     if (myErr == noErr)
  2178.         myErr = AECreateDesc(typeBoolean, (Ptr)&exists, sizeof(exists), &dataDesc);
  2179.  
  2180.     /*
  2181.         if they wanted a reply, which they surely must,
  2182.         attach the result to it…
  2183.     */
  2184.  
  2185.     if (myErr == noErr)
  2186.         if (reply->descriptorType != typeNull)
  2187.              myErr = AEPutParamDesc(reply, keyDirectObject, &dataDesc);
  2188.  
  2189.     if (dataDesc.dataHandle)
  2190.         ignoreErr = AEDisposeDesc(&dataDesc);
  2191.  
  2192.     if (myDirObject.dataHandle)
  2193.         ignoreErr = AEDisposeDesc(&myDirObject);
  2194.  
  2195.     if (windDesc.dataHandle)
  2196.         ignoreErr = AEDisposeDesc(&windDesc);
  2197.  
  2198.     return myErr;
  2199. }    /* DoIsThereA */
  2200.  
  2201. /** -----------------------------------------------------------------------
  2202.         Name:         DoCloseWindow
  2203.         Purpose:        Handles the Close AppleEvent.
  2204.      -----------------------------------------------------------------------**/
  2205.  
  2206. pascal OSErr DoCloseWindow(
  2207.     const AppleEvent *theAppleEvent,
  2208.     AppleEvent       *reply,
  2209.     long             handlerRefCon)
  2210. {
  2211. #pragma unused (reply, handlerRefCon)
  2212.  
  2213.     OSErr         myErr;
  2214.     OSErr         tempErr;
  2215.     AEDesc        myDirObj;
  2216.     AEDesc        newDesc;
  2217.     WindowToken   theWindowToken;
  2218.     Size          tokenSize;
  2219.     DescType      saveOpt;
  2220.     Size          actSize;
  2221.     DescType      returnedType;
  2222.     DPtr          myDPtr;
  2223.  
  2224.     myDirObj.dataHandle = nil;
  2225.  
  2226.     /* pick up the direct object, which is the object (window) to close */
  2227.  
  2228.     myErr = AEGetParamDesc(theAppleEvent, keyDirectObject, typeWildCard, &myDirObj);
  2229.  
  2230.     /* pick up optional save param, if any */
  2231.  
  2232.     saveOpt = kAEAsk; /* the default */
  2233.  
  2234.     tempErr =
  2235.         AEGetParamPtr(
  2236.             theAppleEvent,
  2237.             keyAESaveOptions,
  2238.             typeEnumerated,
  2239.             &returnedType,
  2240.             (Ptr)&saveOpt,
  2241.             sizeof(saveOpt),
  2242.             &actSize);
  2243.  
  2244.     if (myErr == noErr)
  2245.         myErr = GotRequiredParams(theAppleEvent);
  2246.  
  2247.     /* get the window to close as a window ptr */
  2248.     if (myErr == noErr)
  2249.         if (!AECoerceDesc(&myDirObj, typeMyWndw, &newDesc))
  2250.             if (newDesc.descriptorType!=typeNull)  {
  2251.                 GetRawDataFromDescriptor(
  2252.                     &newDesc,
  2253.                     (Ptr)&theWindowToken,
  2254.                     sizeof(theWindowToken),
  2255.                     &tokenSize);
  2256.  
  2257.                 myErr = AEDisposeDesc(&newDesc);
  2258.  
  2259.                 if (theWindowToken) {
  2260.                     myErr=AESetInteractionAllowed(kAEInteractWithAll); /* Should do this in prefs */
  2261.  
  2262.                     /*
  2263.                         We do some of the close checks here to avoid
  2264.                         calling AEInteractWithUser
  2265.                     */
  2266.                     myDPtr = DPtrFromWindowPtr(theWindowToken);
  2267.                     if (myDPtr->kind == kDocumentWindow && (myDPtr->dirty || !myDPtr->u.reg.everSaved))
  2268.                         if (saveOpt != kAENo) /* Don't flip layers if force no ask */
  2269.                             myErr = AEInteractWithUser(kAEDefaultTimeout, nil, nil);
  2270.  
  2271.                     if (myErr==noErr)
  2272.                         myErr = DoClose(theWindowToken, true, saveOpt);
  2273.                 } else
  2274.                     myErr =  errAEIllegalIndex;
  2275.             }
  2276.  
  2277.     if (myDirObj.dataHandle)
  2278.         tempErr = AEDisposeDesc(&myDirObj);
  2279.  
  2280.     return myErr;
  2281. }    /* DoCloseWindow */
  2282.  
  2283. /** -----------------------------------------------------------------------
  2284.         Name:             DoSaveWindow
  2285.         Purpose:        Handles the Save AppleEvent.
  2286.      -----------------------------------------------------------------------**/
  2287.  
  2288. pascal OSErr DoSaveWindow(
  2289.     const AppleEvent *theAppleEvent,
  2290.     AppleEvent       *reply,
  2291.     long             handlerRefCon)
  2292. {
  2293. #pragma unused (reply, handlerRefCon)
  2294.  
  2295.     OSErr         myErr;
  2296.     OSErr         tempErr;
  2297.     AEDesc        myDirObj;
  2298.     AEDesc        newDesc;
  2299.     WindowToken   theWindowToken;
  2300.     Size          tokenSize;
  2301.     Size          actSize;
  2302.     DescType      returnedType;
  2303.     DPtr          theDoc;
  2304.     FSSpec        destFSSpec;
  2305.  
  2306.     myDirObj.dataHandle = nil;
  2307.  
  2308.     /* pick up the direct object, which is the window to save */
  2309.  
  2310.     myErr = AEGetParamDesc(theAppleEvent, keyDirectObject, typeWildCard,  &myDirObj);
  2311.  
  2312.     /* pick up optional destination param, if any */
  2313.  
  2314.     tempErr =
  2315.         AEGetParamPtr(
  2316.             theAppleEvent,
  2317.             keyAEDestination,
  2318.               typeFSS,
  2319.             &returnedType,
  2320.             (Ptr)&destFSSpec,
  2321.             sizeof(destFSSpec),
  2322.             &actSize);
  2323.  
  2324.     if (myErr == noErr)
  2325.         myErr = GotRequiredParams(theAppleEvent);
  2326.  
  2327.     /* get the data */
  2328.  
  2329.     myErr = AECoerceDesc(&myDirObj, typeMyWndw, &newDesc);
  2330.  
  2331.     if (myErr == noErr)
  2332.         if (newDesc.descriptorType!=typeNull) {
  2333.             GetRawDataFromDescriptor(
  2334.                 &newDesc,
  2335.                 (Ptr)&theWindowToken,
  2336.                 sizeof(theWindowToken),
  2337.                 &tokenSize);
  2338.  
  2339.             myErr = AEDisposeDesc(&newDesc);
  2340.  
  2341.             if (theWindowToken) {
  2342.                 theDoc = DPtrFromWindowPtr(theWindowToken);
  2343.  
  2344.                 if (theDoc->kind != kDocumentWindow || theDoc->u.reg.everSaved == false)
  2345.                     if (tempErr != noErr)
  2346.                          /* We had no supplied destination and no default either */
  2347.                         myErr = kAEGenericErr;
  2348.  
  2349.                 if (myErr==noErr)
  2350.                     if (tempErr==noErr) { /* we were told where */
  2351.                         myErr = DoSave(theDoc, destFSSpec);
  2352.  
  2353.                         if (myErr==noErr)
  2354.                             AssocAllSections(theDoc);
  2355.                     } else
  2356.                         myErr = SaveUsingTemp(theDoc);
  2357.             } else
  2358.                 myErr =  errAEIllegalIndex;
  2359.         }
  2360.  
  2361.     if (myDirObj.dataHandle)
  2362.         tempErr = AEDisposeDesc(&myDirObj);
  2363.  
  2364.     return myErr;
  2365. }    /* DoSaveWindow */
  2366.  
  2367. /** -----------------------------------------------------------------------
  2368.         Name:         DoRevertWindow
  2369.         Purpose:        Handles the Revert AppleEvent.
  2370.      -----------------------------------------------------------------------**/
  2371.  
  2372. pascal OSErr DoRevertWindow(
  2373.     const AppleEvent *theAppleEvent,
  2374.     AppleEvent       *reply,
  2375.     long             handlerRefCon)
  2376. {
  2377. #pragma unused (reply, handlerRefCon)
  2378.  
  2379.     OSErr          myErr;
  2380.     OSErr          ignoreErr;
  2381.     AEDesc         myDirObj;
  2382.     AEDesc         newDesc;
  2383.     WindowToken    theWindowToken;
  2384.     Size           tokenSize;
  2385.     DPtr           theDoc;
  2386.  
  2387.     myDirObj.dataHandle = nil;
  2388.  
  2389.   /* pick up the direct object, which is the window to save */
  2390.  
  2391.       if (myErr = AEGetParamDesc(theAppleEvent, keyDirectObject, typeWildCard, &myDirObj))
  2392.           return myErr;
  2393.  
  2394.       GotRequiredParams(theAppleEvent);
  2395.  
  2396.   /* get the window to revert from the direct object */
  2397.  
  2398.     myErr = AECoerceDesc(&myDirObj, typeMyWndw, &newDesc);
  2399.  
  2400.       if (myErr == noErr)
  2401.         if (newDesc.descriptorType!=typeNull) {
  2402.             GetRawDataFromDescriptor(
  2403.                 &newDesc,
  2404.                 (Ptr)&theWindowToken,
  2405.                 sizeof(theWindowToken),
  2406.                 &tokenSize);
  2407.  
  2408.             myErr = AEDisposeDesc(&newDesc);
  2409.  
  2410.             if (theWindowToken) {
  2411.                 theDoc = DPtrFromWindowPtr(theWindowToken);
  2412.  
  2413.                 if (theDoc->kind != kDocumentWindow)
  2414.                     myErr = errAEEventNotHandled;
  2415.                 else {
  2416.                     HidePen();
  2417.                     TESetSelect(0, (*(theDoc->theText))->teLength, theDoc->theText);
  2418.                     ShowPen();
  2419.                     TEDelete(theDoc->theText);
  2420.     
  2421.                     if (theDoc->u.reg.everSaved) {
  2422.                         myErr = GetFileContents(theDoc->theFSSpec, theDoc);
  2423.                         if (myErr == noErr) {
  2424.                             ResizeWindow(theDoc);
  2425.                             theDoc->dirty = false;
  2426.                         }
  2427.                     }
  2428.     
  2429.                     DoShowWindow(theDoc->theWindow); /* <<< Visible already??? */
  2430.                     DoUpdate(theDoc);
  2431.                 }
  2432.             } else
  2433.                 myErr =  errAEIllegalIndex;
  2434.         }
  2435.  
  2436.     if (myDirObj.dataHandle)
  2437.         ignoreErr = AEDisposeDesc(&myDirObj);
  2438.  
  2439.   return myErr;
  2440. }    /* DoRevertWindow */
  2441.  
  2442. #endif
  2443.  
  2444. /**-----------------------------------------------------------------------
  2445.         Name:         DoPrintDocuments
  2446.         Purpose:        Print a list of documents (or windows).
  2447. -----------------------------------------------------------------------**/
  2448. pascal OSErr DoPrintDocuments(
  2449.     const AppleEvent *message,
  2450.    AppleEvent       *reply,
  2451.     long refcon)
  2452. {
  2453. #pragma unused (reply, refcon)
  2454.     long          index;
  2455.     long          itemsInList;
  2456.     AEKeyword     keywd;
  2457.     OSErr         err;
  2458.     AEDescList    docList;
  2459.     Size          actSize;
  2460.     DescType      typeCode;
  2461.     FSSpec        theFSSpec;
  2462.     WindowToken   theWindowToken;
  2463.     OSErr         forgetErr;
  2464.     Boolean       talkToUser;
  2465.  
  2466.     err = AEGetParamDesc(message, keyDirectObject, typeAEList, &docList);
  2467.     err = AECountItems(&docList, &itemsInList);
  2468.  
  2469.     for (index = 1; index<=itemsInList; index++)
  2470.         if (err == noErr) {
  2471.             forgetErr =
  2472.                 AEGetNthPtr(
  2473.                     &docList,
  2474.                     index,
  2475.                     typeFSS,
  2476.                     &keywd,
  2477.                     &typeCode,
  2478.                     (Ptr)&theFSSpec,
  2479.                     sizeof(theFSSpec),
  2480.                     &actSize);
  2481.  
  2482.             if (forgetErr == noErr) {
  2483.                 if (err == noErr)
  2484.                     err = IssueAEOpenDoc(theFSSpec);
  2485.  
  2486.                 if (err == noErr)
  2487.                     IssuePrintWindow(FrontWindow());
  2488.  
  2489.                 if (err == noErr)
  2490.                     IssueCloseCommand(FrontWindow());
  2491.             } else { /* wasn't a file - was it a window ? */
  2492.                 err =
  2493.                     AEGetNthPtr(
  2494.                         &docList,
  2495.                         index,
  2496.                         typeMyWndw,
  2497.                         &keywd,
  2498.                         &typeCode,
  2499.                         (Ptr)&theWindowToken,
  2500.                         sizeof(WindowToken),
  2501.                         &actSize);
  2502.  
  2503.                 talkToUser = (AEInteractWithUser(kAEDefaultTimeout, nil, nil) == noErr);
  2504.  
  2505.                 if (err == noErr)
  2506.                     PrintWindow(DPtrFromWindowPtr(theWindowToken), talkToUser);
  2507.             }
  2508.         }
  2509.  
  2510.     if (docList.dataHandle)
  2511.         forgetErr = AEDisposeDesc(&docList);
  2512.  
  2513.     return err;
  2514. } /* DoPrintDocuments */
  2515.  
  2516. #ifndef RUNTIME
  2517.  
  2518. /**-----------------------------------------------------------------------
  2519.         Name:         HandleCreatePub
  2520.         Purpose:        Create a publisher.
  2521. -----------------------------------------------------------------------**/
  2522. pascal OSErr HandleCreatePub(
  2523.     const AppleEvent *theAppleEvent,
  2524.    AppleEvent       *reply,
  2525.     long refcon)
  2526. {
  2527. #pragma unused (reply, refcon)
  2528.  
  2529.     OSErr       myErr;
  2530.     FSSpec      theFSSpec;
  2531.     OSErr       forgetErr;
  2532.     OSErr       forget2Err;
  2533.     AEDesc      myDirObj;
  2534.     AEDesc      myFileLoc;
  2535.     TextToken   theTextToken;
  2536.     DPtr        theDoc;
  2537.     AEDesc      newDesc;
  2538.     long        tokenSize;
  2539.     Boolean     haveFSSpec;
  2540.  
  2541.     myErr = noErr;
  2542.  
  2543.     forgetErr  = AEGetParamDesc(theAppleEvent, keyDirectObject, typeWildCard, &myDirObj);
  2544.     forget2Err = AEGetParamDesc(theAppleEvent, keyAEEditionFileLoc, typeWildCard, &myFileLoc);
  2545.  
  2546.     if (myErr==noErr)
  2547.         myErr = GotRequiredParams(theAppleEvent);
  2548.  
  2549.     if (forgetErr==noErr) { /* Set the selection to the supplied object - if any */
  2550.         forgetErr = AECoerceDesc(&myDirObj, typeMyText, &newDesc);
  2551.         if (newDesc.descriptorType!=typeNull) {
  2552.             GetRawDataFromDescriptor(&newDesc,
  2553.                                                              (Ptr)&theTextToken,
  2554.                                                              sizeof(theTextToken),
  2555.                                                              &tokenSize);
  2556.  
  2557.             theDoc = DPtrFromWindowPtr(theTextToken.tokenWindow);
  2558.  
  2559.             TESetSelect(theTextToken.tokenOffset-1,
  2560.                                     theTextToken.tokenOffset+
  2561.                                                         theTextToken.tokenLength-1,
  2562.                                     theDoc->theText);
  2563.  
  2564.             forgetErr = AEDisposeDesc(&newDesc);
  2565.         }
  2566.     } else
  2567.         theDoc = DPtrFromWindowPtr(FrontWindow());
  2568.  
  2569.     if (theDoc==nil) {
  2570.         /* Should clean up and exit with error */
  2571.     }
  2572.  
  2573.     haveFSSpec = false;
  2574.  
  2575.     if (forget2Err==noErr) { /* Get the Edition Container File */
  2576.         forget2Err = AECoerceDesc(&myDirObj,typeFSS,&newDesc);
  2577.         if (newDesc.descriptorType!=typeNull) {
  2578.             GetRawDataFromDescriptor(&newDesc, (Ptr)&theFSSpec, sizeof(theFSSpec), &tokenSize);
  2579.             forget2Err = AEDisposeDesc(&newDesc);
  2580.             haveFSSpec = true;
  2581.         }
  2582.     }
  2583.  
  2584.     if (haveFSSpec==false)
  2585.         myErr = GetEditionContainer(theDoc, &theFSSpec);
  2586.  
  2587.     if (myErr == noErr)
  2588.         myErr = PublishText(theDoc, &theFSSpec);
  2589.  
  2590.     if (myDirObj.dataHandle)
  2591.         forgetErr = AEDisposeDesc(&myDirObj);
  2592.  
  2593.     if (myFileLoc.dataHandle)
  2594.         forgetErr = AEDisposeDesc(&myFileLoc);
  2595.  
  2596.     return myErr;
  2597. } /* HandleCreatePub */
  2598.  
  2599.  
  2600. pascal OSErr MyCountProc(
  2601.     DescType desiredType,
  2602.     DescType containerClass,
  2603.     const AEDesc *container,
  2604.     long *result);
  2605.  
  2606. /** -----------------------------------------------------------------------
  2607.         Name:       HandleNumberOfElements
  2608.         Purpose:        Handles the Number Of Elements AppleEvent.
  2609.      -----------------------------------------------------------------------**/
  2610.  
  2611. pascal OSErr HandleNumberOfElements(
  2612.     const AppleEvent *theAppleEvent,
  2613.     AppleEvent *reply,
  2614.     long       handlerRefCon)
  2615. {
  2616. #pragma unused (handlerRefCon)
  2617.  
  2618.       OSErr    myErr;
  2619.       OSErr    forgetErr;
  2620.     AEDesc   myDirObj;
  2621.     DescType myClass;
  2622.     long     myCount;
  2623.     DescType returnedType;
  2624.     Size     actSize;
  2625.  
  2626.     myErr                        = errAEEventNotHandled;
  2627.     myDirObj.dataHandle     = nil;
  2628.  
  2629.     /* pick up direct object, which is the container in which things are to be counted */
  2630.  
  2631.     myErr = AEGetParamDesc(theAppleEvent, keyDirectObject, typeWildCard, &myDirObj);
  2632.  
  2633.     /* now the class of objects to be counted */
  2634.  
  2635.     myErr =
  2636.         AEGetParamPtr(
  2637.             theAppleEvent,
  2638.             keyAEObjectClass,
  2639.             typeType,
  2640.             &returnedType,
  2641.             (Ptr)&myClass,
  2642.             sizeof(myClass),
  2643.             &actSize);
  2644.  
  2645.     /* missing any parameters? */
  2646.  
  2647.     myErr = GotRequiredParams(theAppleEvent);
  2648.  
  2649.     /* now count */
  2650.  
  2651.     if (myErr == noErr)
  2652.         myErr = MyCountProc(myClass,myDirObj.descriptorType, &myDirObj,&myCount);
  2653.  
  2654.     /* add result to reply */
  2655.  
  2656.     if (myErr == noErr)
  2657.         if (reply->descriptorType != typeNull)
  2658.              myErr  =
  2659.                  AEPutParamPtr(
  2660.                     reply,
  2661.                     keyDirectObject,
  2662.                     typeLongInteger,
  2663.                     (Ptr)&myCount,
  2664.                     sizeof(myCount));
  2665.     if (myErr == noErr)
  2666.         forgetErr  = AEDisposeDesc(&myDirObj);
  2667.  
  2668.     return myErr;
  2669. }    /* HandleNumberOfElements */
  2670.  
  2671. /** -----------------------------------------------------------------------
  2672.         Name:             HandleShowSelection
  2673.         Purpose:        Handles the Make Selection Visible AppleEvent.
  2674.      -----------------------------------------------------------------------**/
  2675.  
  2676. pascal OSErr HandleShowSelection(
  2677.     const AppleEvent *theAppleEvent,
  2678.     AppleEvent       *reply,
  2679.     long             handlerRefCon)
  2680. {
  2681. #pragma unused (reply,handlerRefCon)
  2682.  
  2683.     OSErr       myErr;
  2684.     OSErr       ignoreErr;
  2685.     AEDesc      myDirObj;
  2686.     AEDesc      newDesc;
  2687.     AEDesc      tokenDesc;
  2688.     Size        actSize;
  2689.     WindowToken theWindowToken;
  2690.     DPtr        theDocument;
  2691.     TEHandle    theHTE;
  2692.  
  2693.     myErr      = errAEEventNotHandled;
  2694.     myDirObj.dataHandle  = nil;
  2695.     tokenDesc.dataHandle = nil;
  2696.  
  2697.     /*
  2698.         pick up direct object, i.e. the window in which to show the selection
  2699.     */
  2700.  
  2701.     myErr  =
  2702.         AEGetParamDesc(
  2703.             theAppleEvent,
  2704.             keyDirectObject,
  2705.             typeWildCard,
  2706.             &myDirObj);
  2707.  
  2708.     /*
  2709.         missing any parameters?
  2710.     */
  2711.  
  2712.     myErr = GotRequiredParams(theAppleEvent);
  2713.  
  2714.     /*
  2715.         convert object to WindowToken which we understand
  2716.     */
  2717.     myErr = AEResolve(&myDirObj, kAEIDoMinimum, &tokenDesc);
  2718.  
  2719.     if (myErr == noErr)
  2720.         if (tokenDesc.descriptorType==typeMyWndw) {
  2721.             if (AECoerceDesc(&myDirObj, typeMyWndw, &newDesc) == noErr) {
  2722.                 GetRawDataFromDescriptor(
  2723.                     &newDesc,
  2724.                     (Ptr)&theWindowToken,
  2725.                     sizeof(theWindowToken),
  2726.                     &actSize);
  2727.  
  2728.                 ignoreErr = AEDisposeDesc(&newDesc);
  2729.  
  2730.                 if (myErr==noErr)
  2731.                     if (theWindowToken)
  2732.                         ShowSelect(DPtrFromWindowPtr(theWindowToken));
  2733.                     else
  2734.                         myErr = errAEIllegalIndex;
  2735.             }
  2736.         } else if (tokenDesc.descriptorType==typeMyText) {
  2737.             myErr =
  2738.                 SetSelectionOfAppleEventObject(
  2739.                     keyDirectObject,
  2740.                     theAppleEvent,
  2741.                     &theDocument,
  2742.                     &theHTE);
  2743.  
  2744.             if (theDocument)
  2745.               ShowSelect(theDocument);
  2746.             else
  2747.                 myErr = errAEIllegalIndex;
  2748.         } else
  2749.             myErr = errAEEventNotHandled;
  2750.  
  2751.     if (myDirObj.dataHandle)
  2752.         ignoreErr = AEDisposeDesc(&myDirObj);
  2753.  
  2754.     if (tokenDesc.dataHandle)
  2755.         ignoreErr = AEDisposeDesc(&tokenDesc);
  2756.  
  2757.     return myErr;
  2758. }    /* HandleShowSelection */
  2759.  
  2760. pascal OSErr HandleStartRecording(
  2761.     const AppleEvent *theAppleEvent,
  2762.     AppleEvent *reply,
  2763.     long       handlerRefCon)
  2764. {
  2765. #pragma unused (reply,handlerRefCon)
  2766.  
  2767.     OSErr myErr;
  2768.  
  2769.     gBigBrother++;
  2770.  
  2771.     myErr = GotRequiredParams(theAppleEvent);
  2772.  
  2773.     return myErr;
  2774.  
  2775. }    /* HandleStartRecording */
  2776.  
  2777. pascal OSErr HandleStopRecording(
  2778.     const AppleEvent *theAppleEvent,
  2779.     AppleEvent *reply,
  2780.     long handlerRefCon)
  2781. {
  2782. #pragma unused (theAppleEvent,reply,handlerRefCon)
  2783.  
  2784.     gBigBrother--;
  2785.     return noErr;
  2786. }    /* HandleStopRecording */
  2787.  
  2788.  
  2789. #pragma segment AECommandIssuers
  2790.  
  2791. /*******************************************************************************/
  2792. /*
  2793.         Start of section involved in building and sending AppleEvent Objects as/with
  2794.         commands
  2795.  */
  2796.  
  2797. /*
  2798.     Make an AEDesc that describes the selection in the window and text edit
  2799.     record supplied
  2800. */
  2801.  
  2802. pascal OSErr MakeWindowObj(
  2803.     WindowPtr theWindow,
  2804.     AEDesc    *dMyDoc)
  2805. {
  2806.     AEDesc   dNull;
  2807.     AEDesc   dDocName;
  2808.     Str255   windowName;
  2809.     OSErr    myErr;
  2810.  
  2811.     GetWTitle(theWindow, windowName);
  2812.     myErr = AECreateDesc(typeChar,(Ptr)&windowName[1], windowName[0], &dDocName);
  2813.  
  2814.     if (myErr==noErr)
  2815.         myErr = AECreateDesc(typeNull, nil , 0, &dNull);
  2816.  
  2817.     if (myErr==noErr)
  2818.         myErr = CreateObjSpecifier(cWindow, &dNull, formName, &dDocName, true, dMyDoc);
  2819.  
  2820.     return myErr;
  2821. } /*MakeWindowObj*/
  2822.  
  2823. pascal OSErr MakeTextObj(
  2824.     WindowPtr theWindow,
  2825.     short     selStart,
  2826.     short     selEnd,
  2827.     AEDesc    *selTextObj)
  2828. {
  2829.     OSErr    myErr;
  2830.     OSErr    ignoreErr;
  2831.     AEDesc   dMyDoc;
  2832.     AEDesc   startOfs;
  2833.     AEDesc   endOfs;
  2834.     AEDesc   startObj;
  2835.     AEDesc   endObj;
  2836.     AEDesc   rangeDesc;
  2837.     long     startChar;
  2838.     long     endChar;
  2839.     Boolean  spotFlag;
  2840.  
  2841.     myErr = noErr;
  2842.  
  2843.     if (theWindow==nil)
  2844.         return noErr;
  2845.  
  2846.     selTextObj->dataHandle = nil;
  2847.     dMyDoc.dataHandle      = nil;
  2848.     startObj.dataHandle    = nil;
  2849.     endObj.dataHandle      = nil;
  2850.  
  2851.     /*
  2852.         make the window object
  2853.     */
  2854.  
  2855.     if (myErr = MakeWindowObj(theWindow, &dMyDoc))
  2856.         return myErr;
  2857.  
  2858.     /* get the start and end of selection */
  2859.  
  2860.     startChar = selStart+1;    /* start counting obj's from 1, not 0 */
  2861.     endChar   = selEnd;
  2862.     spotFlag  = (selStart == selEnd);
  2863.  
  2864.     if (myErr = CreateOffsetDescriptor(startChar, &startOfs))
  2865.         return noErr;
  2866.  
  2867.     if (spotFlag)
  2868.         myErr =
  2869.             CreateObjSpecifier(
  2870.                 cSpot,
  2871.                 &dMyDoc,
  2872.                 formAbsolutePosition,
  2873.                 &startOfs,
  2874.                 true,
  2875.                 selTextObj);
  2876.     else {
  2877.         /* not a spot - must represent as range */
  2878.         /* make obj for start char */
  2879.  
  2880.         myErr =
  2881.             CreateObjSpecifier(
  2882.                 cChar,
  2883.                 &dMyDoc,
  2884.                 formAbsolutePosition,
  2885.                 &startOfs,
  2886.                 false,
  2887.                 &startObj);
  2888.  
  2889.         if (myErr==noErr)
  2890.             myErr = CreateOffsetDescriptor(endChar, &endOfs);
  2891.  
  2892.         if (myErr==noErr)
  2893.             myErr =
  2894.                 CreateObjSpecifier(
  2895.                     cChar,
  2896.                     &dMyDoc,
  2897.                     formAbsolutePosition,
  2898.                     &endOfs,
  2899.                     false,
  2900.                     &endObj);
  2901.  
  2902.         if (myErr==noErr)
  2903.             myErr = CreateRangeDescriptor(&startObj, &endObj, false, &rangeDesc);
  2904.  
  2905.         if (myErr==noErr)
  2906.             myErr = CreateObjSpecifier(cChar, &dMyDoc, formRange, &rangeDesc, true, selTextObj);
  2907.  
  2908.         if (startObj.dataHandle)
  2909.           ignoreErr = AEDisposeDesc(&startObj);
  2910.  
  2911.         if (startOfs.dataHandle)
  2912.           ignoreErr = AEDisposeDesc(&startOfs);
  2913.  
  2914.         if (endObj.dataHandle)
  2915.           ignoreErr = AEDisposeDesc(&endObj);
  2916.  
  2917.         if (endOfs.dataHandle)
  2918.           ignoreErr = AEDisposeDesc(&endOfs);
  2919.     }
  2920.  
  2921.     return myErr;
  2922. }
  2923.  
  2924. pascal OSErr MakeSelectedTextObj(
  2925.     WindowPtr theWindow,
  2926.     TEHandle  theTextEditHandle,
  2927.     AEDesc    *selTextObj)
  2928. {
  2929.     return
  2930.         MakeTextObj(
  2931.             theWindow,
  2932.             (**theTextEditHandle).selStart,
  2933.             (**theTextEditHandle).selEnd,
  2934.             selTextObj);
  2935.  
  2936. }    /* MakeSelectedTextObj */
  2937.  
  2938. enum editCommandType {
  2939. editCutCommand   = 1,
  2940. editCopyCommand  = 2,
  2941. editPasteCommand = 3,
  2942. editClearCommand = 4
  2943. };
  2944.  
  2945. typedef enum editCommandType editCommandType;
  2946.  
  2947. pascal void DoEditCommand(DPtr theDocument,editCommandType whatCommand)
  2948. {
  2949.       OSErr         err;
  2950.       OSErr         forgetErr;
  2951.     AEAddressDesc ourAddress;
  2952.     AppleEvent    editCommandEvent;
  2953.     AppleEvent    ignoreReply;
  2954.     AEDesc        ourTextSelObj;
  2955.     AEEventID     theEventID;
  2956.     AEEventClass  theEventClass;
  2957.  
  2958.     /*
  2959.             Initialise
  2960.     */
  2961.  
  2962.     ourAddress.dataHandle             = nil;
  2963.     ourTextSelObj.dataHandle         = nil;
  2964.     editCommandEvent.dataHandle     = nil;
  2965.     ignoreReply.dataHandle             = nil;
  2966.  
  2967.     err = MakeSelfAddress(&ourAddress);
  2968.  
  2969.     /*
  2970.         Build an object to represent the current document's selection
  2971.     */
  2972.     err = MakeSelectedTextObj(theDocument->theWindow, theDocument->theText, &ourTextSelObj);
  2973.  
  2974.     if (err==noErr) {
  2975.         switch (whatCommand) {
  2976.         case  editCutCommand:
  2977.             theEventID    = kAECut;
  2978.             theEventClass = kAEMiscStandards;
  2979.             break;
  2980.         case  editCopyCommand:
  2981.             theEventID    = kAECopy;
  2982.             theEventClass = kAEMiscStandards;
  2983.             break;
  2984.         case  editPasteCommand:
  2985.             theEventID    = kAEPaste;
  2986.             theEventClass = kAEMiscStandards;
  2987.             break;
  2988.         case  editClearCommand:
  2989.             theEventID    = kAEDelete;
  2990.             theEventClass = kAECoreSuite;
  2991.             break;
  2992.         }
  2993.  
  2994.         err = AECreateAppleEvent( theEventClass, theEventID, &ourAddress, 0, 0, &editCommandEvent);
  2995.  
  2996.         /* add parameter */
  2997.         if (err==noErr)
  2998.             err = AEPutParamDesc(&editCommandEvent, keyDirectObject, &ourTextSelObj);
  2999.  
  3000.         /*and now Send the message*/
  3001.         if (err==noErr)
  3002.             err = AESend(&editCommandEvent,&ignoreReply,kAENoReply,kAEHighPriority,10000,nil, nil);
  3003.     }
  3004.  
  3005.     /*
  3006.         Clean up
  3007.     */
  3008.     if (ourAddress.dataHandle)
  3009.         forgetErr = AEDisposeDesc(&ourAddress);
  3010.  
  3011.     if (editCommandEvent.dataHandle)
  3012.         forgetErr = AEDisposeDesc(&editCommandEvent);
  3013.  
  3014.     if (ignoreReply.dataHandle)
  3015.         forgetErr = AEDisposeDesc(&ignoreReply);
  3016.  
  3017.     if (ourTextSelObj.dataHandle)
  3018.         forgetErr = AEDisposeDesc(&ourTextSelObj);
  3019.  
  3020. } /*DoEditCommand*/
  3021.  
  3022. #endif
  3023.  
  3024. pascal void IssueCutCommand(DPtr theDocument)
  3025. {
  3026. #ifndef RUNTIME
  3027.     DoEditCommand(theDocument, editCutCommand);
  3028. #else
  3029.     if (theDocument->kind != kDocumentWindow)
  3030.         if (    !theDocument->u.cons.selected 
  3031.             || (*theDocument->theText)->selStart < theDocument->u.cons.fence
  3032.         ) {
  3033.             if (AllSelected(theDocument->theText)) {
  3034.                 if (theDocument->u.cons.fence < 32767)
  3035.                     theDocument->u.cons.fence = 0;
  3036.             } else {
  3037.                 SysBeep(1);
  3038.                 
  3039.                 IssueCopyCommand(theDocument);
  3040.                 
  3041.                 return;
  3042.             }
  3043.         }
  3044.  
  3045.     ZeroScrap();
  3046.     TECut(theDocument->theText);
  3047.     TEToScrap();
  3048.     AdjustScrollbars(theDocument, false);
  3049.     DrawPageExtras(theDocument);
  3050.     theDocument->dirty = true;
  3051. #endif
  3052. }
  3053.  
  3054. pascal void IssueCopyCommand(DPtr theDocument)
  3055. {
  3056. #ifndef RUNTIME
  3057.     DoEditCommand(theDocument, editCopyCommand);
  3058. #else
  3059.     ZeroScrap();
  3060.     TECopy(theDocument->theText);
  3061.     TEToScrap();
  3062. #endif
  3063. }
  3064.  
  3065. pascal void IssuePasteCommand(DPtr theDocument)
  3066. {
  3067. #ifndef RUNTIME
  3068.     DoEditCommand(theDocument, editPasteCommand);
  3069. #else
  3070.     if (theDocument->kind != kDocumentWindow)
  3071.         if (    !theDocument->u.cons.selected
  3072.             || (*theDocument->theText)->selStart < theDocument->u.cons.fence
  3073.         ) {
  3074.             SysBeep(1);
  3075.             
  3076.             return;
  3077.         }
  3078.     
  3079.     TEFromScrap();
  3080.     TEPaste(theDocument->theText);
  3081.     EnforceMemory(theDocument, theDocument->theText);
  3082.     AdjustScrollbars(theDocument, false);
  3083.     DrawPageExtras(theDocument);
  3084.         
  3085.     theDocument->dirty = true;
  3086. #endif
  3087. }
  3088.  
  3089. pascal void IssueClearCommand(DPtr theDocument)
  3090. {
  3091. #ifndef RUNTIME
  3092.     DoEditCommand(theDocument, editClearCommand);
  3093. #else
  3094.     if (theDocument->kind != kDocumentWindow)
  3095.         if (    !theDocument->u.cons.selected
  3096.             || (*theDocument->theText)->selStart < theDocument->u.cons.fence
  3097.         ) {
  3098.             if (AllSelected(theDocument->theText)) {
  3099.                 if (theDocument->u.cons.fence < 32767)
  3100.                     theDocument->u.cons.fence = 0;
  3101.             } else {
  3102.                 SysBeep(1);
  3103.             
  3104.                 return;
  3105.             }
  3106.         }
  3107.         
  3108.     TEDelete(theDocument->theText);
  3109.     AdjustScrollbars(theDocument, false);
  3110.     DrawPageExtras(theDocument);
  3111.     theDocument->dirty = true;
  3112. #endif
  3113. }
  3114.  
  3115. /*
  3116.     Window property routines
  3117. */
  3118.  
  3119. pascal void IssueZoomCommand(WindowPtr whichWindow, short whichPart)
  3120. {
  3121. #ifndef RUNTIME
  3122.       Boolean       zoomBool;
  3123.     AEDesc        zoomDesc;
  3124.     AEAddressDesc selfAddr;
  3125.     AEDesc        frontWinObj;
  3126.     OSErr         err;
  3127.  
  3128.     err = MakeSelfAddress(&selfAddr);
  3129.     err = MakeWindowObj(whichWindow, &frontWinObj);
  3130.  
  3131.     zoomBool = (whichPart==inZoomOut);
  3132.  
  3133.     err = AECreateDesc(typeBoolean, (Ptr)&zoomBool, sizeof(zoomBool), &zoomDesc);
  3134.     err = SendAESetObjProp(&frontWinObj, pIsZoomed, &zoomDesc, &selfAddr);
  3135. #else
  3136.     ZoomWindow(whichWindow, whichPart, false);
  3137.  
  3138.     ResizeWindow(DPtrFromWindowPtr(whichWindow));
  3139. #endif
  3140. } /* IssueZoomCommand */
  3141.  
  3142. pascal void IssueCloseCommand(WindowPtr whichWindow)
  3143. {
  3144. #ifndef RUNTIME
  3145.     AEAddressDesc  selfAddr;
  3146.     AEDesc         frontWinObj;
  3147.     OSErr          err;
  3148.     OSErr          ignoreErr;
  3149.     AppleEvent     closeCommandEvent;
  3150.     AppleEvent     ignoreReply;
  3151.  
  3152.     frontWinObj.dataHandle = nil;
  3153.  
  3154.     err = MakeSelfAddress(&selfAddr);
  3155.     err = MakeWindowObj(whichWindow, &frontWinObj);
  3156.     err = AECreateAppleEvent( kAECoreSuite, kAEClose, &selfAddr, 0, 0, &closeCommandEvent) ;
  3157.  
  3158.     /* add parameter - the window to close */
  3159.     if (err==noErr)
  3160.         err = AEPutParamDesc(&closeCommandEvent, keyDirectObject, &frontWinObj);
  3161.  
  3162.     if (err==noErr)
  3163.         err = AESend(&closeCommandEvent,&ignoreReply,kAENoReply+kAEAlwaysInteract,kAEHighPriority,10000,nil, nil);
  3164.  
  3165.     if (closeCommandEvent.dataHandle)
  3166.         ignoreErr = AEDisposeDesc(&closeCommandEvent);
  3167.  
  3168.     if (selfAddr.dataHandle)
  3169.         ignoreErr = AEDisposeDesc(&selfAddr);
  3170.  
  3171.     if (frontWinObj.dataHandle)
  3172.         ignoreErr = AEDisposeDesc(&frontWinObj);
  3173. #else
  3174.     DoClose(whichWindow, true, kAEAsk);
  3175. #endif
  3176. } /* IssueCloseCommand */
  3177.  
  3178. pascal void IssueSizeWindow(WindowPtr whichWindow, short newHSize, short newVSize)
  3179. {
  3180. #ifndef RUNTIME
  3181.       Rect          sizeRect;
  3182.     Rect          contentRect;
  3183.     short         edgeSize;
  3184.     AEDesc        sizeDesc;
  3185.     AEAddressDesc selfAddr;
  3186.     AEDesc        frontWinObj;
  3187.     OSErr         err;
  3188.  
  3189.     sizeRect    = (**(((WindowPeek)whichWindow)->strucRgn)).rgnBBox;
  3190.     contentRect = (**(((WindowPeek)whichWindow)->contRgn)).rgnBBox;
  3191.  
  3192.     edgeSize = sizeRect.right-sizeRect.left-(contentRect.right-contentRect.left);
  3193.     sizeRect.right = sizeRect.left+newHSize+edgeSize;
  3194.  
  3195.     edgeSize = sizeRect.bottom-sizeRect.top-(contentRect.bottom-contentRect.top);
  3196.     sizeRect.bottom = sizeRect.top+newVSize+edgeSize;
  3197.  
  3198.     err = MakeSelfAddress(&selfAddr);
  3199.  
  3200.     err = MakeWindowObj(whichWindow, &frontWinObj);
  3201.  
  3202.     if (err==noErr)
  3203.         err =
  3204.             AECreateDesc(
  3205.                 typeQDRectangle,
  3206.                 (Ptr)&sizeRect,
  3207.                 sizeof(sizeRect),
  3208.                 &sizeDesc);
  3209.  
  3210.     if (err==noErr)
  3211.         err =
  3212.             SendAESetObjProp(
  3213.                 &frontWinObj,
  3214.                 pBounds,
  3215.                 &sizeDesc,
  3216.                 &selfAddr);
  3217. #else
  3218.     ResizeWindow(DPtrFromWindowPtr(whichWindow));
  3219. #endif
  3220. } /*IssueSizeWindow*/
  3221.  
  3222. pascal void IssueMoveWindow(WindowPtr whichWindow, Rect sizeRect)
  3223. {
  3224. #ifndef RUNTIME
  3225.     AEDesc        sizeDesc;
  3226.     AEAddressDesc selfAddr;
  3227.     AEDesc        frontWinObj;
  3228.     OSErr         err;
  3229.  
  3230.     err = MakeSelfAddress(&selfAddr);
  3231.     err = MakeWindowObj(whichWindow, &frontWinObj);
  3232.  
  3233.     if (err==noErr)
  3234.         err = AECreateDesc(typeQDRectangle, (Ptr)&sizeRect, sizeof(sizeRect), &sizeDesc);
  3235.  
  3236.     if (err==noErr)
  3237.         err = SendAESetObjProp(&frontWinObj, pBounds, &sizeDesc, &selfAddr);
  3238. #else
  3239.     /* Everything is all right already */
  3240. #endif
  3241. } /*IssueMoveWindow*/
  3242.  
  3243. pascal void IssuePageSetupWindow(WindowPtr whichWindow, TPrint thePageSetup)
  3244. {
  3245. #ifndef RUNTIME
  3246.     AEDesc        sizeDesc;
  3247.     AEAddressDesc selfAddr;
  3248.     AEDesc        frontWinObj;
  3249.     OSErr         err;
  3250.  
  3251.     err = MakeSelfAddress(&selfAddr);
  3252.  
  3253.     err = MakeWindowObj(whichWindow, &frontWinObj);
  3254.  
  3255.     if (err==noErr)
  3256.         err = AECreateDesc(typeTPrint, (Ptr)&thePageSetup, sizeof(thePageSetup), &sizeDesc);
  3257.  
  3258.     if (err==noErr)
  3259.         err = SendAESetObjProp(&frontWinObj, pPageSetup, &sizeDesc, &selfAddr);
  3260. #else
  3261.     /* Everything is all right already */
  3262. #endif
  3263. } /*IssuePageSetupWindow*/
  3264.  
  3265. #ifndef RUNTIME
  3266. pascal void IssueShowBorders(WindowPtr whichWindow, Boolean showBorders)
  3267. {
  3268.     AEDesc        sizeDesc;
  3269.     AEAddressDesc selfAddr;
  3270.     AEDesc        frontWinObj;
  3271.     OSErr         err;
  3272.  
  3273.     err = MakeSelfAddress(&selfAddr);
  3274.  
  3275.     err = MakeWindowObj(whichWindow, &frontWinObj);
  3276.  
  3277.     if (err==noErr)
  3278.         err = AECreateDesc(typeBoolean, (Ptr)&showBorders, sizeof(showBorders), &sizeDesc);
  3279.  
  3280.     if (err==noErr)
  3281.         err = SendAESetObjProp(&frontWinObj, pShowBorders, &sizeDesc, &selfAddr);
  3282. } /*IssueShowBorders*/
  3283. #endif
  3284.  
  3285. pascal void IssuePrintWindow(WindowPtr whichWindow)
  3286. {
  3287. #ifndef RUNTIME
  3288.     AEAddressDesc selfAddr;
  3289.     AEDesc        frontWinObj;
  3290.     OSErr         err;
  3291.     OSErr         ignoreErr;
  3292.     AppleEvent    printCommandEvent;
  3293.     AppleEvent    ignoreReply;
  3294.  
  3295.     err = MakeSelfAddress(&selfAddr);
  3296.  
  3297.     err = MakeWindowObj(whichWindow, &frontWinObj);
  3298.  
  3299.     err = AECreateAppleEvent(kCoreEventClass, kAEPrintDocuments, &selfAddr, 0, 0, &printCommandEvent) ;
  3300.  
  3301.     /*
  3302.         add parameter - the window to print
  3303.     */
  3304.  
  3305.     if (err==noErr)
  3306.         err = AEPutParamDesc(&printCommandEvent, keyDirectObject, &frontWinObj);
  3307.  
  3308.     if (err==noErr)
  3309.         err = AESend(&printCommandEvent,&ignoreReply,kAENoReply+kAEAlwaysInteract,kAEHighPriority,10000,nil, nil);
  3310.  
  3311.       if (printCommandEvent.dataHandle)
  3312.         ignoreErr = AEDisposeDesc(&printCommandEvent);
  3313.  
  3314.     if (frontWinObj.dataHandle)
  3315.         err = AEDisposeDesc(&frontWinObj);
  3316.  
  3317.     if (selfAddr.dataHandle)
  3318.         err = AEDisposeDesc(&selfAddr);
  3319. #else
  3320.     PrintWindow(DPtrFromWindowPtr(whichWindow), true);
  3321. #endif
  3322. } /*IssuePrintWindow*/
  3323.  
  3324. pascal OSErr IssueAEOpenDoc(FSSpec myFSSpec)
  3325. /* send OpenDocs AppleEvent to myself, with a one-element list
  3326.   containing the given file spec
  3327.  
  3328.   NOTES : the core AEOpenDocs event is defined as taking a list of
  3329.           aliases (not file specs) as its direct parameter.  However,
  3330.             we can send the file spec instead and depend on AppleEvents'
  3331.             automatic coercion.  In fact, we don't really even have to put
  3332.             in a list; AppleEvents will coerce a descriptor into a 1-element
  3333.             list if called for.  In this routine, though, we'll make the
  3334.             list for demonstration purposes.
  3335. */
  3336.  
  3337. {
  3338. #ifndef RUNTIME
  3339.     AppleEvent    myAppleEvent;
  3340.     AppleEvent    defReply;
  3341.     AEDescList    docList;
  3342.     AEAddressDesc selfAddr;
  3343.     OSErr         myErr;
  3344.     OSErr         ignoreErr;
  3345.  
  3346.     myAppleEvent.dataHandle = nil;
  3347.     docList.dataHandle  = nil;
  3348.     selfAddr.dataHandle = nil;
  3349.     defReply.dataHandle = nil;
  3350.  
  3351.     /*
  3352.         Create empty list and add one file spec
  3353.     */
  3354.     myErr = AECreateList(nil,0,false, &docList);
  3355.  
  3356.     if (myErr==noErr)
  3357.         myErr = AEPutPtr(&docList,1,typeFSS,(Ptr)&myFSSpec,sizeof(myFSSpec));
  3358.  
  3359.     /*
  3360.         Create a self address to send it to
  3361.     */
  3362.     if (myErr==noErr)
  3363.         myErr = MakeSelfAddress(&selfAddr);
  3364.  
  3365.     if (myErr==noErr)
  3366.         myErr =
  3367.             AECreateAppleEvent(
  3368.                 MPAppSig,
  3369.                 kAEOpenDocuments,
  3370.                 &selfAddr,
  3371.                 kAutoGenerateReturnID,
  3372.                 kAnyTransactionID,
  3373.                 &myAppleEvent);
  3374.  
  3375.     /*
  3376.         Put Params into our event and send it
  3377.     */
  3378.     if (myErr == noErr)
  3379.         myErr =
  3380.             AEPutParamDesc(
  3381.                 &myAppleEvent,
  3382.                 keyDirectObject,
  3383.                 &docList);
  3384.  
  3385.     myErr =
  3386.         AESend(
  3387.             &myAppleEvent,
  3388.             &defReply,
  3389.             kAENoReply+kAEAlwaysInteract,
  3390.             kAENormalPriority,
  3391.             kAEDefaultTimeout,
  3392.             nil,
  3393.             nil);
  3394.  
  3395.     if (selfAddr.dataHandle)
  3396.         ignoreErr = AEDisposeDesc(&selfAddr);
  3397.  
  3398.     if (myAppleEvent.dataHandle)
  3399.         ignoreErr = AEDisposeDesc(&myAppleEvent);
  3400.  
  3401.     if (docList.dataHandle)
  3402.         ignoreErr = AEDisposeDesc(&docList);
  3403.  
  3404.     return myErr;
  3405. #else
  3406.     DocType        type;
  3407.  
  3408.     switch (type = GetDocType(&myFSSpec)) {
  3409.     case kPlainTextDoc:
  3410.     case kScriptDoc:
  3411.     case kRuntime6Doc:
  3412.     case kOldRuntime6Doc:
  3413.     case kRuntime7Doc:
  3414.         return OpenOld(myFSSpec, type);
  3415.     default:
  3416.         SysBeep(1);
  3417.         
  3418.         return errAEEventNotHandled;
  3419.     }
  3420. #endif
  3421. }    /* IssueAEOpenDoc */
  3422.  
  3423. pascal void IssueAENewWindow(void)
  3424. /*
  3425.     send the New Element event to myself with a null container
  3426. */
  3427. {
  3428. #ifndef RUNTIME
  3429.     AppleEvent    myAppleEvent;
  3430.     AppleEvent    defReply;
  3431.     AEAddressDesc selfAddr;
  3432.     OSErr         myErr;
  3433.     OSErr         ignoreErr;
  3434.     DescType      elemClass;
  3435.  
  3436.     myAppleEvent.dataHandle = nil;
  3437.  
  3438.     /*
  3439.         Create the address of us
  3440.     */
  3441.  
  3442.     myErr = MakeSelfAddress(&selfAddr);
  3443.  
  3444.     /*
  3445.         create event
  3446.     */
  3447.  
  3448.     myErr =
  3449.         AECreateAppleEvent(
  3450.             kAECoreSuite,
  3451.             kAECreateElement,
  3452.             &selfAddr,
  3453.             kAutoGenerateReturnID,
  3454.             kAnyTransactionID,
  3455.             &myAppleEvent);
  3456.  
  3457.     /*
  3458.         attach desired class of new element
  3459.     */
  3460.  
  3461.     elemClass = cWindow;
  3462.  
  3463.     if (myErr == noErr)
  3464.         myErr =
  3465.             AEPutParamPtr(
  3466.                 &myAppleEvent,
  3467.                 keyAEObjectClass,
  3468.                 typeType,
  3469.                 (Ptr)&elemClass,
  3470.                 sizeof(elemClass));
  3471.  
  3472.     /*
  3473.         send the event
  3474.     */
  3475.  
  3476.     if (myErr == noErr)
  3477.         myErr =
  3478.             AESend(
  3479.                 &myAppleEvent,
  3480.                 &defReply,
  3481.                 kAENoReply+kAENeverInteract,
  3482.                 kAENormalPriority,
  3483.                 kAEDefaultTimeout,
  3484.                 nil,
  3485.                 nil);
  3486.     /*
  3487.         Clean up - reply never created so don't throw away
  3488.     */
  3489.     if (selfAddr.dataHandle)
  3490.         ignoreErr = AEDisposeDesc(&selfAddr);
  3491.  
  3492.     if (myAppleEvent.dataHandle)
  3493.         ignoreErr = AEDisposeDesc(&myAppleEvent);
  3494. #else
  3495.     DPtr    doc;
  3496.     
  3497.     doc = NewDocument(false, kDocumentWindow);
  3498.  
  3499.     if (doc)
  3500.         DoShowWindow(doc->theWindow);
  3501. #endif
  3502. }    /* IssueAENewWindow */
  3503.  
  3504. pascal OSErr IssueSaveCommand(
  3505.     WindowPtr theWindow,
  3506.     FSSpecPtr where)
  3507. /*
  3508.     send an AppleEvent Save Event to myself
  3509. */
  3510. {
  3511. #ifndef RUNTIME
  3512.     AEDesc        windowObj;
  3513.     AppleEvent    myAppleEvent;
  3514.     AppleEvent    defReply;
  3515.     OSErr         myErr;
  3516.     OSErr         ignoreErr;
  3517.     AEAddressDesc selfAddr;
  3518.  
  3519.     windowObj.dataHandle = nil;
  3520.     myAppleEvent.dataHandle = nil;
  3521.  
  3522.     myErr = MakeWindowObj(theWindow, &windowObj);
  3523.  
  3524.     if (myErr==noErr)
  3525.         myErr = MakeSelfAddress(&selfAddr);
  3526.  
  3527.   /*
  3528.         Build event
  3529.     */
  3530.   if (myErr == noErr)
  3531.         myErr =
  3532.             AECreateAppleEvent(
  3533.                 kAECoreSuite,
  3534.                 kAESave,
  3535.                 &selfAddr,
  3536.                 kAutoGenerateReturnID,
  3537.                 kAnyTransactionID,
  3538.                 &myAppleEvent);
  3539.  
  3540.   /*
  3541.         say which window
  3542.     */
  3543.   if (myErr==noErr)
  3544.         myErr = AEPutParamDesc(&myAppleEvent, keyDirectObject, &windowObj);
  3545.  
  3546.   /*
  3547.         add optional file param if we need to
  3548.     */
  3549.   if (where)
  3550.         if (myErr==noErr)
  3551.             myErr =
  3552.                 AEPutParamPtr(
  3553.                     &myAppleEvent,
  3554.                     keyAEDestination,
  3555.                     typeFSS,
  3556.                     (Ptr)where,
  3557.                     sizeof(FSSpec));
  3558.  
  3559.   /*
  3560.         send the event
  3561.     */
  3562.   if (myErr==noErr)
  3563.         myErr  =
  3564.             AESend(
  3565.                 &myAppleEvent,
  3566.                 &defReply,
  3567.                 kAENoReply+kAENeverInteract,
  3568.                 kAENormalPriority,
  3569.                 kAEDefaultTimeout,
  3570.                 nil,
  3571.                 nil);
  3572.  
  3573.     if (selfAddr.dataHandle)
  3574.         ignoreErr = AEDisposeDesc(&selfAddr);
  3575.  
  3576.     if (windowObj.dataHandle)
  3577.         ignoreErr = AEDisposeDesc(&windowObj);
  3578.  
  3579.     if (myAppleEvent.dataHandle)
  3580.         myErr = AEDisposeDesc(&myAppleEvent);
  3581.  
  3582.     return myErr;
  3583. #else
  3584.     if (where)
  3585.         return DoSave(DPtrFromWindowPtr(theWindow), *where);
  3586.     else
  3587.         return SaveUsingTemp(DPtrFromWindowPtr(theWindow));
  3588. #endif
  3589. }    /* IssueSaveCommand */
  3590.  
  3591. pascal OSErr IssueRevertCommand(WindowPtr theWindow)
  3592. /*
  3593.     send an AppleEvent Revert Event to myself
  3594. */
  3595. {
  3596. #ifndef RUNTIME
  3597.     AEDesc        windowObj;
  3598.     AppleEvent    myAppleEvent;
  3599.     AppleEvent    defReply;
  3600.     OSErr         myErr;
  3601.     OSErr         ignoreErr;
  3602.     AEAddressDesc selfAddr;
  3603.  
  3604.     windowObj.dataHandle = nil;
  3605.     myAppleEvent.dataHandle = nil;
  3606.  
  3607.     myErr = MakeWindowObj(theWindow, &windowObj);
  3608.  
  3609.     if (myErr==noErr)
  3610.         myErr = MakeSelfAddress(&selfAddr);
  3611.  
  3612.     /*
  3613.         Build event
  3614.     */
  3615.  
  3616.     if (myErr == noErr)
  3617.         myErr  =
  3618.             AECreateAppleEvent(
  3619.                 kAEMiscStandards,
  3620.                 kAERevert,
  3621.                 &selfAddr,
  3622.                 kAutoGenerateReturnID,
  3623.                 kAnyTransactionID,
  3624.                 &myAppleEvent);
  3625.     /*
  3626.         say which window
  3627.     */
  3628.  
  3629.     if (myErr == noErr)
  3630.         myErr = AEPutParamDesc(&myAppleEvent, keyDirectObject, &windowObj);
  3631.     /*
  3632.         send the event
  3633.     */
  3634.     if (myErr==noErr)
  3635.         myErr =
  3636.             AESend(
  3637.                 &myAppleEvent,
  3638.                 &defReply,
  3639.                 kAENoReply+kAENeverInteract,
  3640.                 kAENormalPriority,
  3641.                 kAEDefaultTimeout,
  3642.                 nil,
  3643.                 nil);
  3644.  
  3645.     if (windowObj.dataHandle)
  3646.         ignoreErr = AEDisposeDesc(&windowObj);
  3647.  
  3648.     if (myAppleEvent.dataHandle)
  3649.         ignoreErr = AEDisposeDesc(&myAppleEvent);
  3650.  
  3651.     if (selfAddr.dataHandle)
  3652.         ignoreErr = AEDisposeDesc(&selfAddr);
  3653.  
  3654.     return myErr;
  3655. #else
  3656.     OSErr myErr;
  3657.     DPtr     theDoc = DPtrFromWindowPtr(theWindow);
  3658.  
  3659.     if (theDoc->kind != kDocumentWindow)
  3660.         return errAEEventNotHandled;
  3661.     else {
  3662.         HidePen();
  3663.         TESetSelect(0, (*(theDoc->theText))->teLength, theDoc->theText);
  3664.         ShowPen();
  3665.         TEDelete(theDoc->theText);
  3666.  
  3667.         if (theDoc->u.reg.everSaved) {
  3668.             myErr = GetFileContents(theDoc->theFSSpec, theDoc);
  3669.             if (myErr == noErr) {
  3670.                 ResizeWindow(theDoc);
  3671.                 theDoc->dirty = false;
  3672.             }
  3673.         }
  3674.  
  3675.         DoShowWindow(theDoc->theWindow); /* <<< Visible already??? */
  3676.         DoUpdate(theDoc);
  3677.         
  3678.         return noErr;
  3679.     }
  3680. #endif
  3681. }    /* IssueRevertCommand */
  3682.  
  3683. /*
  3684.     Name : IssueQuitCommand
  3685.     Purpose : Sends self a Quit AppleEvent
  3686. */
  3687. pascal OSErr IssueQuitCommand(void)
  3688. {
  3689. #ifndef RUNTIME
  3690.     AppleEvent    myAppleEvent;
  3691.     AppleEvent    defReply;
  3692.     OSErr         myErr;
  3693.     OSErr         ignoreErr;
  3694.     AEAddressDesc selfAddr;
  3695.     DescType      mySaveOpt;
  3696.  
  3697.     myAppleEvent.dataHandle = nil;
  3698.     selfAddr.dataHandle     = nil;
  3699.  
  3700.     myErr = MakeSelfAddress(&selfAddr);
  3701.  
  3702.     /*
  3703.         Build event
  3704.     */
  3705.     if (myErr == noErr)
  3706.         myErr  =
  3707.             AECreateAppleEvent(
  3708.                 kCoreEventClass,
  3709.                 kAEQuitApplication,
  3710.                 &selfAddr,
  3711.                 kAutoGenerateReturnID,
  3712.                 kAnyTransactionID,
  3713.                 &myAppleEvent);
  3714.     /*
  3715.         say which save option
  3716.     */
  3717.     mySaveOpt = kAEAsk;
  3718.  
  3719.     if (myErr == noErr)
  3720.         myErr =
  3721.             AEPutParamPtr(
  3722.                 &myAppleEvent,
  3723.                 keyAESaveOptions,
  3724.                 typeEnumerated,
  3725.                 (Ptr)&mySaveOpt,
  3726.                 sizeof(mySaveOpt));
  3727.     /*
  3728.         send the event
  3729.     */
  3730.     if (myErr==noErr)
  3731.         myErr =
  3732.             AESend(
  3733.                 &myAppleEvent,
  3734.                 &defReply,
  3735.                 kAENoReply+kAEAlwaysInteract,
  3736.                 kAENormalPriority,
  3737.                 kAEDefaultTimeout,
  3738.                 nil,
  3739.                 nil);
  3740.  
  3741.     if (myAppleEvent.dataHandle)
  3742.         ignoreErr = AEDisposeDesc(&myAppleEvent);
  3743.  
  3744.     if (selfAddr.dataHandle)
  3745.         ignoreErr = AEDisposeDesc(&selfAddr);
  3746.  
  3747.     return myErr;
  3748. #else
  3749.     DoQuit(kAEAsk);
  3750.     
  3751.     return noErr;
  3752. #endif
  3753. }    /* IssueQuitCommand */
  3754.  
  3755. #ifndef RUNTIME
  3756.  
  3757. /*
  3758.      Name :IssueCreatePublisher
  3759.      Purpose :Interact with user to get Publisher info
  3760.                         and the IssueAECommand to Publish currect selection
  3761. */
  3762. pascal void IssueCreatePublisher(DPtr whichDoc)
  3763. {
  3764.     AEAddressDesc selfAddr;
  3765.     AEDesc        selTextObj;
  3766.     OSErr         err;
  3767.     OSErr         ignoreErr;
  3768.     AppleEvent    publishCommandEvent;
  3769.     AppleEvent    ignoreReply;
  3770.  
  3771.       publishCommandEvent.dataHandle = nil;
  3772.     selfAddr.dataHandle = nil;
  3773.     selTextObj.dataHandle = nil;
  3774.  
  3775.     err = MakeSelfAddress(&selfAddr);
  3776.  
  3777.     if (err==noErr)
  3778.         err = MakeSelectedTextObj(whichDoc->theWindow, whichDoc->theText, &selTextObj);
  3779.  
  3780.     err =
  3781.         AECreateAppleEvent(
  3782.             kAEMiscStandards,
  3783.             kAECreatePublisher,
  3784.             &selfAddr,
  3785.             0,
  3786.             0,
  3787.             &publishCommandEvent) ;
  3788.  
  3789.     /*
  3790.         add parameter - the text to publish
  3791.     */
  3792.     if (err==noErr)
  3793.         err = AEPutParamDesc(&publishCommandEvent, keyDirectObject, &selTextObj);
  3794.  
  3795.     if (err==noErr)
  3796.         err =
  3797.             AESend(
  3798.                 &publishCommandEvent,
  3799.                 &ignoreReply,
  3800.                 kAENoReply+kAEAlwaysInteract,
  3801.                 kAEHighPriority,
  3802.                 10000,
  3803.                 nil,
  3804.                 nil);
  3805.  
  3806.       if (publishCommandEvent.dataHandle)
  3807.         ignoreErr = AEDisposeDesc(&publishCommandEvent);
  3808.  
  3809.     if (selTextObj.dataHandle)
  3810.         ignoreErr = AEDisposeDesc(&selTextObj);
  3811.  
  3812.     if (selfAddr.dataHandle)
  3813.         ignoreErr = AEDisposeDesc(&selfAddr);
  3814. } /*IssueCreatePublisher*/
  3815.  
  3816. #endif
  3817.  
  3818. #define kOK 1
  3819. #define kCancel 2
  3820. #define kOtherSize 4
  3821. #define kOutlineItem 5
  3822.  
  3823. pascal Boolean PoseSizeDialog(long *whatSize)
  3824. {
  3825.     GrafPtr   savedPort;
  3826.     DialogPtr aDialog;
  3827.     Str255    aString;
  3828.     short     itemHit;
  3829.  
  3830.     GetPort(&savedPort);
  3831.     aDialog = GetNewDialog(1004, nil, (WindowPtr)-1);
  3832.     DoShowWindow(aDialog);
  3833.     SetPort(aDialog);
  3834.  
  3835.     AdornDefaultButton(aDialog, kOutlineItem);
  3836.  
  3837.     /*set the edittext button to contain the right size*/
  3838.     NumToString(*whatSize, aString);
  3839.     SetText(aDialog, kOtherSize, aString);
  3840.  
  3841.     do {
  3842.         ModalDialog(nil, &itemHit);
  3843.     } while ((itemHit!=kOK) && (itemHit!=kCancel));
  3844.  
  3845.     if (itemHit == kOK)
  3846.         RetrieveText(aDialog, kOtherSize, aString);
  3847.  
  3848.     DisposDialog(aDialog);
  3849.     SetPort(savedPort);
  3850.  
  3851.     if (itemHit == kOK) {
  3852.         /*set the new size of the text*/
  3853.         StringToNum(aString, whatSize);
  3854.         if ((*whatSize<1) || (*whatSize>2000))
  3855.              *whatSize = 12;
  3856.     }
  3857.     return itemHit == kOK;
  3858. }
  3859.  
  3860. pascal void IssueFormatCommand(DPtr theDocument)
  3861. {
  3862.     Str255            name;
  3863. #ifndef RUNTIME
  3864.     AEDesc            desc;
  3865.     AEAddressDesc     theAddress;
  3866.     AEDesc            windowObj;
  3867. #endif
  3868.     OSErr             err;
  3869.     DocFormat        fmt;
  3870.     Boolean            defaultFormat;
  3871.     
  3872.     if (theDocument) {
  3873.         fmt.font         =     (*theDocument->theText)->txFont;
  3874.         fmt.size         =     (*theDocument->theText)->txSize;
  3875.         defaultFormat    =    false;
  3876.     } else {
  3877.         fmt                 =     gFormat;
  3878.         defaultFormat    =    true;
  3879.     }
  3880.     
  3881.     if (DoFormatDialog(&fmt, &defaultFormat)) {
  3882.         if (theDocument) {
  3883. #ifndef RUNTIME
  3884.             err = MakeSelfAddress(&theAddress);
  3885.             err = MakeWindowObj(theDocument->theWindow, &windowObj);
  3886.  
  3887.             if (err==noErr)
  3888.                   err = CreateOffsetDescriptor(fmt.size, &desc);
  3889.  
  3890.             if (err==noErr)
  3891.                 err = SendAESetObjProp(&windowObj, pPointSize, &desc, &theAddress);
  3892.                 
  3893.             err = MakeSelfAddress(&theAddress);
  3894.             err = MakeWindowObj(theDocument->theWindow, &windowObj);
  3895.         
  3896.             GetFontName(fmt.font, name);
  3897.         
  3898.             if (err==noErr)
  3899.                 err  = AECreateDesc(typeChar, (Ptr)&name[1], name[0], &desc);
  3900.         
  3901.             if (err==noErr)
  3902.                 err  = SendAESetObjProp(&windowObj, pFont, &desc, &theAddress);
  3903. #else
  3904.             (*theDocument->theText)->txFont = fmt.font;
  3905.             (*theDocument->theText)->txSize = fmt.size;
  3906.             RecalcFontInfo(theDocument->theText);
  3907.             AdjustScrollbars(theDocument, false);
  3908.             DrawPageExtras(theDocument);
  3909.             
  3910.             if (theDocument->kind == kDocumentWindow)
  3911.                 theDocument->dirty = true;
  3912.  
  3913.             if (theDocument->theWindow == FrontWindow() && !gInBackground)
  3914.                 AdjustScript(theDocument);
  3915. #endif
  3916.         }
  3917.         
  3918.         if (defaultFormat) {
  3919.             gFormat = fmt;
  3920.  
  3921.             if (gPrefsFile) {
  3922.                 short        resFile;
  3923.                 short    **    defaultFont;
  3924.             
  3925.                 resFile = CurResFile();
  3926.                 UseResFile(gPrefsFile);
  3927.                 
  3928.                 defaultFont = (short **) Get1Resource('PFNT', 128);
  3929.                 **defaultFont = gFormat.size;
  3930.                 GetFontName(gFormat.font, name);
  3931.                 SetResInfo((Handle) defaultFont, 128, name);
  3932.                 ChangedResource((Handle) defaultFont);
  3933.                 WriteResource((Handle) defaultFont);
  3934.                 UpdateResFile(gPrefsFile);
  3935.             
  3936.                 UseResFile(resFile);
  3937.             }
  3938.         }
  3939.     }
  3940. } /*IssueFormatCommand*/
  3941.  
  3942. #ifndef RUNTIME
  3943.  
  3944. pascal OSErr IssueSetDataObjToBufferContents(const AEDesc * theObj)
  3945. {
  3946.       OSErr             myErr;
  3947.     OSErr                ignoreErr;
  3948.     AEAddressDesc     theAddress;
  3949.     AppleEvent        myAppleEvent;
  3950.     AppleEvent        defReply;
  3951.  
  3952.     myErr = MakeSelfAddress(&theAddress);
  3953.  
  3954.     /* create event */
  3955.  
  3956.     if (myErr==noErr)
  3957.         myErr = AECreateAppleEvent(kAECoreSuite, kAESetData, &theAddress, 0, 0, &myAppleEvent);
  3958.  
  3959.     /* add prop obj spec to the event */
  3960.  
  3961.     if (myErr==noErr)
  3962.         myErr = AEPutParamDesc(&myAppleEvent, keyDirectObject, theObj);
  3963.  
  3964.     /* add prop data to the event */
  3965.  
  3966.     if (myErr==noErr)
  3967.         myErr =
  3968.             AEPutParamPtr(
  3969.                 &myAppleEvent,
  3970.                 keyAEData,
  3971.                 typeChar,
  3972.                 (Ptr)gTypingBuffer,
  3973.                 gCharsInBuffer);
  3974.  
  3975.     /* send event */
  3976.  
  3977.     if (myErr==noErr)
  3978.      if (gRecordingImplemented)
  3979.          myErr =
  3980.              AESend(
  3981.                 &myAppleEvent,
  3982.                 &defReply,
  3983.                 kAENoReply+kAEDontExecute,
  3984.                 kAENormalPriority,
  3985.                 kAEDefaultTimeout,
  3986.                 nil,
  3987.                 nil);
  3988.  
  3989.     if (theAddress.dataHandle)
  3990.         ignoreErr = AEDisposeDesc(&theAddress);
  3991.  
  3992.     if (myAppleEvent.dataHandle)
  3993.         ignoreErr = AEDisposeDesc(&myAppleEvent);
  3994.  
  3995.     return myErr;
  3996. }
  3997.  
  3998. pascal void AddKeyToTypingBuffer(DPtr theDocument, char theKey)
  3999. {
  4000.     OSErr myErr;
  4001.     OSErr ignoreErr;
  4002.  
  4003.     if (theKey==BS || theKey==FS || theKey==GS || theKey==RS || theKey==US) {
  4004.         FlushAndRecordTypingBuffer();
  4005.         if (theKey==BS) {
  4006.             if ((**theDocument->theText).selStart!=(**theDocument->theText).selEnd) {
  4007.                 myErr =
  4008.                     MakeTextObj(
  4009.                         theDocument->theWindow,
  4010.                         (**theDocument->theText).selStart,
  4011.                         (**theDocument->theText).selEnd,
  4012.                         &gTypingTargetObject);
  4013.             } else {
  4014.                 myErr =
  4015.                     MakeTextObj(
  4016.                         theDocument->theWindow,
  4017.                         (**theDocument->theText).selStart-1,
  4018.                         (**theDocument->theText).selStart,
  4019.                         &gTypingTargetObject);
  4020.             }
  4021.  
  4022.              myErr = IssueSetDataObjToBufferContents(&gTypingTargetObject);
  4023.  
  4024.             ignoreErr = AEDisposeDesc(&gTypingTargetObject);
  4025.  
  4026.             gTypingTargetObject.dataHandle = nil;
  4027.         }
  4028.     } else {
  4029.         if (gCharsInBuffer==0)
  4030.             myErr =
  4031.                 MakeSelectedTextObj(
  4032.                     theDocument->theWindow,
  4033.                     theDocument->theText,
  4034.                     &gTypingTargetObject);
  4035.  
  4036.         gTypingBuffer[gCharsInBuffer++] = theKey;
  4037.     }
  4038. }
  4039.  
  4040. pascal void FlushAndRecordTypingBuffer(void)
  4041. {
  4042.       OSErr  myErr;
  4043.     OSErr  ignoreErr;
  4044.  
  4045.     if (gCharsInBuffer != 0) {
  4046.         myErr = IssueSetDataObjToBufferContents(&gTypingTargetObject);
  4047.  
  4048.         if (gTypingTargetObject.dataHandle)
  4049.             ignoreErr = AEDisposeDesc(&gTypingTargetObject);
  4050.     }
  4051.  
  4052.     gCharsInBuffer = 0;
  4053.     gTypingTargetObject.dataHandle = 0;
  4054. }
  4055.  
  4056. /*****************************************************************************/
  4057. /*
  4058.     Object Accessors
  4059. */
  4060.  
  4061. pascal OSErr WindowFromNullAccessor(
  4062.     DescType      wantClass,
  4063.     const AEDesc  *container,
  4064.     DescType      containerClass,
  4065.     DescType      form,
  4066.     const AEDesc  *selectionData,
  4067.     AEDesc        *value,
  4068.     long          theRefCon)
  4069. {
  4070. #pragma unused (container,theRefCon)
  4071.  
  4072.     OSErr       myErr;
  4073.     Str255      nameStr;
  4074.     WindowToken theWindow;
  4075.     short       index;
  4076.     AEDesc      resultDesc;
  4077.  
  4078.     myErr = errAEBadKeyForm;    /* or whatever */
  4079.  
  4080.     value->dataHandle     = nil;
  4081.     resultDesc.dataHandle = nil;
  4082.  
  4083.     /*
  4084.         should only be called with wantClass = cWindow and
  4085.         with containerClass = typeNull or typeMyAppl.
  4086.         Currently accept as either formName or formAbsolutePosition
  4087.     */
  4088.  
  4089.     if (
  4090.         (wantClass != cWindow) ||
  4091.         ((containerClass != typeNull) && (containerClass != typeMyAppl)) ||
  4092.         !((form == formName) || (form == formAbsolutePosition))
  4093.     )
  4094.         return errAEWrongDataType;
  4095.  
  4096.     if (form == formName) {
  4097.         myErr     = GetPStringFromDescriptor(selectionData, (char *)nameStr);
  4098.         theWindow = WindowNameToWindowPtr(nameStr);
  4099.     }
  4100.  
  4101.     if (form == formAbsolutePosition) {
  4102.         myErr         = GetIntegerFromDescriptor(selectionData, &index);
  4103.  
  4104.         if (index<0)
  4105.             index = CountWindows()+index+1;
  4106.  
  4107.         theWindow = GetWindowPtrOfNthWindow(index);
  4108.     }
  4109.  
  4110.     if (myErr == noErr)
  4111.         myErr = AECreateDesc(typeMyWndw, (Ptr)&theWindow, sizeof(theWindow), value);
  4112.  
  4113.     return myErr;
  4114. }    /* WindowFromNullAccessor */
  4115.  
  4116. pascal OSErr ApplicationFromNullAccessor(
  4117.     DescType      wantClass,
  4118.     const AEDesc  *container,
  4119.     DescType      containerClass,
  4120.     DescType      form,
  4121.     const AEDesc  *selectionData,
  4122.     AEDesc        *value,
  4123.     long          theRefCon)
  4124. {
  4125. #pragma unused (container,selectionData,theRefCon)
  4126.  
  4127.     OSErr    myErr;
  4128.     appToken theApp;
  4129.     AEDesc   resultDesc;
  4130.  
  4131.     value->dataHandle     = nil;
  4132.     resultDesc.dataHandle = nil;
  4133.  
  4134.     /*
  4135.         should only be called with wantClass = cWindow and
  4136.         with containerClass = typeNull.
  4137.         Currently accept as either formName or formAbsolutePosition
  4138.     */
  4139.  
  4140.     if (
  4141.         (wantClass != cApplication) ||
  4142.         (containerClass != typeNull) ||
  4143.         !((form == formName) || (form == formAbsolutePosition))
  4144.     )
  4145.         return errAEWrongDataType;
  4146.  
  4147.     if ((form == formName) || (form == formAbsolutePosition)) {
  4148.         theApp.highLongOfPSN = 0;
  4149.         theApp.lowLongOfPSN  = kCurrentProcess;
  4150.     }
  4151.  
  4152.     myErr = AECreateDesc(typeMyAppl, (Ptr)&theApp, sizeof(theApp), value);
  4153.  
  4154.     return myErr;
  4155. }    /* ApplicationFromNullAccessor */
  4156.  
  4157. pascal void MoveToNonSpace(short *start, short limit, charsHandle myChars)
  4158. /*
  4159.     Treats space,comma, full stop, ; and : as space chars
  4160. */
  4161. {
  4162.     while (*start<=limit)
  4163.           switch ((**myChars)[*start]) {
  4164.           case ' ':
  4165.         case ',':
  4166.         case '.':
  4167.         case ':':
  4168.         case 10:
  4169.         case 13:
  4170.             (*start) +=1;
  4171.             break;
  4172.         default:
  4173.             return;
  4174.         }
  4175. }
  4176.  
  4177. pascal void MoveToSpace(short *start, short limit, charsHandle myChars)
  4178.     /*
  4179.         Treats space,comma, full stop, ; and : as space chars
  4180.     */
  4181. {
  4182.     while (*start<=limit)
  4183.           switch ((**myChars)[*start]) {
  4184.           case ' ':
  4185.         case ',':
  4186.         case '.':
  4187.         case ':':
  4188.         case 10:
  4189.         case 13:
  4190.             return;
  4191.         default:
  4192.             (*start) +=1;
  4193.             break;
  4194.         }
  4195. }
  4196.  
  4197. pascal short CountWords(TEHandle inTextHandle, short startAt, short forHowManyChars)
  4198. {
  4199.     charsHandle myChars;
  4200.     short       start;
  4201.     short       limit;
  4202.     short       myWords;
  4203.  
  4204.     myChars  = (charsHandle)(**inTextHandle).hText;
  4205.     limit    = startAt+forHowManyChars-1;
  4206.     start    = startAt;
  4207.     myWords  = 0;
  4208.     MoveToNonSpace(&start, limit, myChars);
  4209.     while (start<=limit) {
  4210.         myWords++;
  4211.         MoveToSpace(&start, limit, myChars);
  4212.         MoveToNonSpace(&start, limit, myChars);
  4213.     }
  4214.     return myWords;
  4215. } /* CountWords */
  4216.  
  4217. pascal void GetNthWordInfo(
  4218.     short    whichWord,
  4219.     TEHandle inTextHandle,
  4220.     short    *wordStartChar,
  4221.     short    *wordLength)
  4222.     /*
  4223.         On entry:    wordStartChar is start of char range to count in
  4224.                             wordLength is number of chars to consider
  4225.  
  4226.         On Exit : wordStartChar is start of requested word
  4227.                             wordLength is number of chars in word
  4228.     */
  4229. {
  4230.     charsHandle myChars;
  4231.     short       start;
  4232.     short       limit;
  4233.  
  4234.     myChars  = (charsHandle)(**inTextHandle).hText;
  4235.     limit    = *wordStartChar + *wordLength-1;
  4236.     start    = *wordStartChar;
  4237.     MoveToNonSpace(&start, limit, myChars);
  4238.     while ((start<=limit) && (whichWord>0)) {
  4239.  
  4240.         whichWord       = whichWord-1;
  4241.         *wordStartChar  = start;
  4242.         MoveToSpace(&start, limit, myChars);
  4243.         *wordLength     = start- *wordStartChar;
  4244.  
  4245.         MoveToNonSpace(&start, limit, myChars);
  4246.     }
  4247. } /* GetNthWordInfo */
  4248.  
  4249. pascal void GetWordInfo(
  4250.     short    whichWord,
  4251.     TEHandle inTextHandle,
  4252.     short    *wordStartChar,
  4253.     short    *wordLength)
  4254.     /*
  4255.         On wordStartChar entry is start of char range to count in
  4256.                             wordLength is number of chars to consider
  4257.  
  4258.         On Exit : wordStartChar is start of requested word
  4259.                             wordLength is number of chars in word
  4260.     */
  4261. {
  4262.     short noOfWords;
  4263.  
  4264.     noOfWords = CountWords(inTextHandle, *wordStartChar, *wordLength);
  4265.  
  4266.     if (whichWord<0)
  4267.         whichWord = noOfWords + whichWord + 1;
  4268.  
  4269.     if (whichWord>noOfWords) {
  4270.         *wordStartChar = *wordStartChar+*wordLength;
  4271.         *wordLength    = 0;
  4272.     } else
  4273.         GetNthWordInfo(whichWord, inTextHandle, wordStartChar, wordLength);
  4274. }
  4275.  
  4276. pascal short CountLines(TEHandle inTextHandle)
  4277. {
  4278.     /*
  4279.         CountLines makes use of info in TERec
  4280.     */
  4281.     return (**inTextHandle).nLines;
  4282. }
  4283.  
  4284. pascal short LineOfOffset(TEHandle theHTE, short charOffset)
  4285. {
  4286.     short n;
  4287.  
  4288.     n = (**theHTE).nLines;
  4289.  
  4290.     while (((**theHTE).lineStarts[n-1]>charOffset) &&
  4291.                  (n>0))
  4292.          n--;
  4293.  
  4294.     return n;
  4295. } /* LineOfOffset */
  4296.  
  4297. pascal void GetLineInfo(
  4298.     short    whichLine,
  4299.     TEHandle inTextHandle,
  4300.     short    *lineStartChar,
  4301.     short    *lineLength)
  4302. {
  4303.     short       noOfLines;
  4304.     charsHandle myChars;
  4305.  
  4306.     /* Addition of lines within text object */
  4307.     short       lineOfStart;
  4308.     short       lineOfEnd;
  4309.  
  4310.     lineOfStart = LineOfOffset(inTextHandle, *lineStartChar);
  4311.     lineOfEnd   = LineOfOffset(inTextHandle, *lineStartChar+*lineLength-1);
  4312.  
  4313.     myChars   = (charsHandle)(**inTextHandle).hText;
  4314.     noOfLines = lineOfEnd - lineOfStart + 1;
  4315.  
  4316.     if (whichLine<0)
  4317.         whichLine = noOfLines + whichLine + 1;
  4318.  
  4319.     noOfLines = CountLines(inTextHandle);
  4320.     whichLine = whichLine + lineOfStart - 1; /* convert offset relative to offset absolute */
  4321.  
  4322.     /* End of addition */
  4323.  
  4324.     if (whichLine<=lineOfEnd) {
  4325.         *lineStartChar = (**inTextHandle).lineStarts[whichLine-1];
  4326.         if (whichLine==noOfLines)
  4327.             *lineLength  = (**inTextHandle).teLength;
  4328.         else
  4329.             *lineLength  = (**inTextHandle).lineStarts[whichLine];
  4330.         *lineLength    = *lineLength-*lineStartChar;
  4331.         /*
  4332.             Don't return CR
  4333.         */
  4334.         if ((**myChars)[ *lineStartChar+*lineLength-1] == 13)
  4335.             *lineLength = *lineLength-1;
  4336.     } else {
  4337.         if (whichLine<noOfLines)
  4338.           *lineStartChar = (**inTextHandle).lineStarts[whichLine]; /* start of whichLine++ */
  4339.         else
  4340.             *lineStartChar = (**inTextHandle).teLength;
  4341.         *lineLength    = 0;
  4342.     }
  4343. } /* GetLineInfo */
  4344.  
  4345. pascal OSErr TextElemFromWndwAccessor(
  4346.     DescType     wantClass,
  4347.     const AEDesc *container,
  4348.     DescType     containerClass,
  4349.     DescType     form,
  4350.     const AEDesc *selectionData,
  4351.     AEDesc       *value,
  4352.     long         theRefCon)
  4353. {
  4354. #pragma unused (theRefCon)
  4355.  
  4356.     OSErr       myErr;
  4357.     OSErr       ignoreErr;
  4358.     WindowToken theWindow;
  4359.     Size        actSize;
  4360.     long        index;
  4361.     TextToken   theTextToken;
  4362.     AERecord    selectionRecord;
  4363.     TextToken   startText;
  4364.     TextToken   stopText;
  4365.     DescType    returnedType;
  4366.     AEDesc      windDesc;
  4367.     TEHandle    theHTE;
  4368.     DPtr        theDocument;
  4369.     short       wordStartChar;
  4370.     short       wordLength;
  4371.  
  4372.     myErr = -1700;    /* or whatever */
  4373.  
  4374.     selectionRecord.dataHandle = nil;
  4375.  
  4376.     /* do some checking for robustness' sake */
  4377.  
  4378.     if (
  4379.         (containerClass != cWindow) ||
  4380.         ((wantClass != cText) && (wantClass != cChar) && (wantClass != cSpot) && (wantClass != cWord) && (wantClass != cLine)    ) ||
  4381.         ((form!=formRange) && (form!=formAbsolutePosition))
  4382.     )
  4383.         return errAEWrongDataType;
  4384.  
  4385.     /* let's get the window which contains the text element */
  4386.  
  4387.     myErr = AECoerceDesc(container, typeMyWndw, &windDesc);
  4388.     GetRawDataFromDescriptor(&windDesc, (Ptr)&theWindow, sizeof(theWindow), &actSize);
  4389.     myErr = AEDisposeDesc(&windDesc);
  4390.  
  4391.     if (theWindow==nil)
  4392.         myErr = errAEIllegalIndex;
  4393.     else {
  4394.         theTextToken.tokenWindow = theWindow;
  4395.  
  4396.         theDocument = DPtrFromWindowPtr(theTextToken.tokenWindow);
  4397.         theHTE      = theDocument->theText;
  4398.  
  4399.         switch (form) {
  4400.         case formAbsolutePosition:
  4401.             myErr = GetLongIntFromDescriptor(selectionData, &index);
  4402.  
  4403.             switch (wantClass) {
  4404.             case cSpot:
  4405.                 if (index<0)
  4406.                     theTextToken.tokenOffset = (**theHTE).teLength+index+2; /* Past last char */
  4407.                 else
  4408.                     theTextToken.tokenOffset = index;
  4409.  
  4410.                 theTextToken.tokenLength = 0;
  4411.                 break;
  4412.  
  4413.             case cChar:
  4414.                 if (index<0)
  4415.                     theTextToken.tokenOffset = (**theHTE).teLength+index+1;
  4416.                 else
  4417.                   theTextToken.tokenOffset = index;
  4418.  
  4419.                 theTextToken.tokenLength = 1;
  4420.                 break;
  4421.  
  4422.             case cWord:
  4423.                 wordStartChar = 0;
  4424.                 wordLength    = (**theHTE).teLength;
  4425.                 GetWordInfo(index, theHTE, &wordStartChar, &wordLength); /* zero based */
  4426.                 theTextToken.tokenOffset = wordStartChar+1;
  4427.                 theTextToken.tokenLength = wordLength;
  4428.                 break;
  4429.  
  4430.             case cLine:
  4431.                 wordStartChar = 0;
  4432.                 wordLength    = (**theHTE).teLength;
  4433.                 GetLineInfo(index, theHTE, &wordStartChar, &wordLength); /* zero based */
  4434.                 theTextToken.tokenOffset = wordStartChar+1;
  4435.                 theTextToken.tokenLength = wordLength;
  4436.                 break;
  4437.             
  4438.             case cText:
  4439.                 theTextToken.tokenOffset = 1;
  4440.                 theTextToken.tokenLength = (**theHTE).teLength;
  4441.                 myErr                             = noErr;
  4442.                 break;
  4443.             }
  4444.             break;
  4445.  
  4446.         case formRange:
  4447.             /* coerce the selection data into an AERecord */
  4448.  
  4449.              myErr = AECoerceDesc(selectionData, typeAERecord, &selectionRecord);
  4450.  
  4451.             /* get the start object as a text token -
  4452.                     this will reenter this proc but as formAbsolutePosition via the coercion handler*/
  4453.  
  4454.             myErr =
  4455.                 AEGetKeyPtr(
  4456.                     &selectionRecord,
  4457.                     keyAERangeStart,
  4458.                     typeMyText,
  4459.                     &returnedType,
  4460.                     (Ptr)&startText,
  4461.                     sizeof(startText),
  4462.                     &actSize);
  4463.  
  4464.             /* now do the same for the stop object */
  4465.             if (myErr==noErr)
  4466.                 myErr =
  4467.                     AEGetKeyPtr(
  4468.                         &selectionRecord,
  4469.                         keyAERangeStop,
  4470.                         typeMyText,
  4471.                         &returnedType,
  4472.                         (Ptr)&stopText,
  4473.                         sizeof(stopText),
  4474.                         &actSize);
  4475.  
  4476.             if (myErr==noErr)
  4477.                 if (
  4478.                     (theTextToken.tokenWindow != stopText.tokenWindow) ||
  4479.                     (theTextToken.tokenWindow != startText.tokenWindow)
  4480.                 )
  4481.                     myErr = errAECorruptData;    /* or whatever ????*/
  4482.  
  4483.             theTextToken.tokenOffset  = startText.tokenOffset;
  4484.             theTextToken.tokenLength  = stopText.tokenOffset + stopText.tokenLength - startText.tokenOffset;
  4485.  
  4486.             if (theTextToken.tokenLength<0)
  4487.                 myErr = errAECorruptData;    /* or whatever */
  4488.  
  4489.             ignoreErr = AEDisposeDesc(&selectionRecord);
  4490.  
  4491.             break;
  4492.         }
  4493.     }
  4494.  
  4495.     /* return theTextToken in a descriptor */
  4496.  
  4497.     if (myErr==noErr)
  4498.         myErr = AECreateDesc(typeMyText, (Ptr)&theTextToken, sizeof(theTextToken), value);
  4499.  
  4500.     return myErr;
  4501. }    /* TextElemFromWndwAccessor */
  4502.  
  4503. pascal OSErr TextElemFromTextAccessor(
  4504.     DescType     wantClass,
  4505.     const AEDesc *container,
  4506.     DescType     containerClass,
  4507.     DescType     form,
  4508.     const AEDesc *selectionData,
  4509.     AEDesc       *value,
  4510.     long         theRefCon)
  4511. {
  4512. #pragma unused (theRefCon, containerClass)
  4513.  
  4514.     OSErr       myErr;
  4515.     Size        actSize;
  4516.     long        index;
  4517.     TextToken   theTextToken;
  4518.     AERecord    selectionRecord;
  4519.     TextToken   startText;
  4520.     TextToken   stopText;
  4521.     DescType    returnedType;
  4522.     AEDesc      textDesc;
  4523.     TEHandle    theHTE;
  4524.     short       wordStartChar;
  4525.     short       wordLength;
  4526.     DPtr        theDocument;
  4527.  
  4528.     myErr = -1700;    /* or whatever */
  4529.  
  4530.     /* do some checking for robustness' sake */
  4531.  
  4532.     if (
  4533.         ((wantClass != cText) && (wantClass != cChar) && (wantClass != cSpot) && (wantClass != cLine) && (wantClass != cWord)) ||
  4534.         ((form != formAbsolutePosition) && (form != formRange))
  4535.     )
  4536.         return errAEWrongDataType;
  4537.  
  4538.     /* let's get the src text */
  4539.  
  4540.     myErr = AECoerceDesc(container, typeMyText, &textDesc);
  4541.     GetRawDataFromDescriptor(&textDesc, (Ptr)&theTextToken, sizeof(theTextToken), &actSize);
  4542.  
  4543.     myErr = AEDisposeDesc(&textDesc);
  4544.  
  4545.     theDocument = DPtrFromWindowPtr(theTextToken.tokenWindow);
  4546.     theHTE      = theDocument->theText;
  4547.  
  4548.     switch (form) {
  4549.     case formAbsolutePosition:
  4550.         myErr = GetLongIntFromDescriptor(selectionData, &index);
  4551.  
  4552.         switch (wantClass) {
  4553.         case cSpot:
  4554.             if (index<0)
  4555.                 theTextToken.tokenOffset = theTextToken.tokenOffset+index+1+theTextToken.tokenLength;
  4556.             else
  4557.                 theTextToken.tokenOffset = theTextToken.tokenOffset+index-1;
  4558.             theTextToken.tokenLength = 0;
  4559.             break;
  4560.  
  4561.         case cChar:
  4562.             if (index<0)
  4563.                 theTextToken.tokenOffset = theTextToken.tokenOffset+index+1+theTextToken.tokenLength;
  4564.             else
  4565.                 theTextToken.tokenOffset = theTextToken.tokenOffset+index-1;
  4566.             theTextToken.tokenLength = 1;
  4567.             break;
  4568.  
  4569.         case cWord:
  4570.             wordStartChar = theTextToken.tokenOffset-1;
  4571.             wordLength    = theTextToken.tokenLength;
  4572.  
  4573.             GetWordInfo(index, theHTE, &wordStartChar, &wordLength);/*zero based*/
  4574.  
  4575.             theTextToken.tokenOffset = wordStartChar+1;
  4576.             theTextToken.tokenLength = wordLength;
  4577.             break;
  4578.  
  4579.         case cLine:
  4580.             wordStartChar = theTextToken.tokenOffset-1;
  4581.             wordLength    = theTextToken.tokenLength;
  4582.  
  4583.             GetLineInfo(index, theHTE, &wordStartChar, &wordLength);
  4584.  
  4585.             theTextToken.tokenOffset = wordStartChar+1;
  4586.             theTextToken.tokenLength = wordLength;
  4587.             break;
  4588.         }
  4589.         break;
  4590.  
  4591.     case formRange:
  4592.         /* coerce the selection data into an AERecord */
  4593.  
  4594.          myErr = AECoerceDesc(selectionData, typeAERecord, &selectionRecord);
  4595.  
  4596.         /* get the start object as a text token -
  4597.                 this will reenter this proc but as formAbsolutePosition via the coercion handler*/
  4598.  
  4599.         myErr =
  4600.             AEGetKeyPtr(
  4601.                 &selectionRecord,
  4602.                 keyAERangeStart,
  4603.                 typeMyText,
  4604.                 &returnedType,
  4605.                 (Ptr)&startText,
  4606.                 sizeof(startText),
  4607.                 &actSize);
  4608.  
  4609.         /* now do the same for the stop object */
  4610.  
  4611.         if (myErr==noErr)
  4612.             myErr =
  4613.                 AEGetKeyPtr(
  4614.                     &selectionRecord,
  4615.                     keyAERangeStop,
  4616.                     typeMyText,
  4617.                     &returnedType,
  4618.                     (Ptr)&stopText,
  4619.                     sizeof(stopText),
  4620.                     &actSize);
  4621.  
  4622.         if (myErr==noErr)
  4623.             if ((theTextToken.tokenWindow != stopText.tokenWindow) ||
  4624.                   (theTextToken.tokenWindow != startText.tokenWindow))
  4625.                 myErr = errAECorruptData;    /* or whatever */
  4626.  
  4627.         theTextToken.tokenOffset  = startText.tokenOffset;
  4628.         theTextToken.tokenLength  = stopText.tokenOffset + stopText.tokenLength - startText.tokenOffset;
  4629.  
  4630.         myErr = AEDisposeDesc(&selectionRecord);
  4631.         break;
  4632.     }
  4633.  
  4634.     /* return theTextToken in a descriptor */
  4635.  
  4636.     myErr =
  4637.         AECreateDesc(
  4638.             typeMyText,
  4639.             (Ptr)&theTextToken,
  4640.             sizeof(theTextToken),
  4641.             value);
  4642.  
  4643.     return myErr;
  4644. }    /* TextElemFromTextAccessor */
  4645.  
  4646. pascal OSErr PropertyFromTextAccessor(
  4647.     DescType     wantClass,
  4648.     const AEDesc *container,
  4649.     DescType     containerClass,
  4650.     DescType     form,
  4651.     const AEDesc *selectionData,
  4652.     AEDesc       *value,
  4653.     long         theRefCon)
  4654. {
  4655. #pragma unused (theRefCon, containerClass)
  4656.  
  4657.     OSErr         myErr;
  4658.     OSErr         ignoreErr;
  4659.     TextToken     theTextToken;
  4660.     DescType      theProperty;
  4661.     AEDesc        textDesc;
  4662.     AEDesc        propDesc;
  4663.     Size          actualSize;
  4664.     textPropToken myTextProp;
  4665.  
  4666.     value->dataHandle   = nil;
  4667.     textDesc.dataHandle = nil;
  4668.     propDesc.dataHandle = nil;
  4669.  
  4670.     if ((wantClass != cProperty) || (form != formPropertyID)) {
  4671.         return errAEWrongDataType;
  4672.     }
  4673.  
  4674.     /* get the text token */
  4675.     myErr = AECoerceDesc(container, typeMyText, &textDesc);
  4676.     GetRawDataFromDescriptor(&textDesc, (Ptr)&theTextToken, sizeof(theTextToken), &actualSize);
  4677.  
  4678.     /* get the property */
  4679.     myErr = AECoerceDesc(selectionData, typeType, &propDesc);
  4680.     GetRawDataFromDescriptor(&propDesc, (Ptr)&theProperty, sizeof(theProperty), &actualSize);
  4681.  
  4682.     /*
  4683.         Combine the two into single token
  4684.     */
  4685.     myTextProp.propertyTextToken = theTextToken;
  4686.     myTextProp.propertyProperty  = theProperty;
  4687.  
  4688.     myErr = AECreateDesc(typeMyTextProp, (Ptr)&myTextProp, sizeof(myTextProp), value);
  4689.  
  4690.     if (textDesc.dataHandle)
  4691.         ignoreErr = AEDisposeDesc(&textDesc);
  4692.  
  4693.     if (propDesc.dataHandle)
  4694.         ignoreErr = AEDisposeDesc(&propDesc);
  4695.  
  4696.     return myErr;
  4697. }    /* PropertyFromTextAccessor */
  4698.  
  4699. pascal OSErr PropertyFromWndwAccessor(
  4700.     DescType     wantClass,
  4701.     const AEDesc *container,
  4702.     DescType     containerClass,
  4703.     DescType     form,
  4704.     const AEDesc *selectionData,
  4705.     AEDesc       *value,
  4706.     long         theRefCon)
  4707. {
  4708. #pragma unused (theRefCon, containerClass)
  4709.  
  4710.     OSErr           myErr;
  4711.     OSErr           ignoreErr;
  4712.     WindowToken     theWindowToken;
  4713.     DescType        theProperty;
  4714.     AEDesc          windowDesc;
  4715.     AEDesc          propDesc;
  4716.     Size            actualSize;
  4717.     windowPropToken myWindowProp;
  4718.  
  4719.     value->dataHandle     = nil;
  4720.     windowDesc.dataHandle = nil;
  4721.     propDesc.dataHandle   = nil;
  4722.  
  4723.     if ((wantClass != cProperty) || (form != formPropertyID)) {
  4724.         return errAEWrongDataType;
  4725.     }
  4726.  
  4727.     /* get the window token - it's the container */
  4728.     myErr = AECoerceDesc(container, typeMyWndw, &windowDesc);
  4729.     GetRawDataFromDescriptor(&windowDesc, (Ptr)&theWindowToken, sizeof(theWindowToken), &actualSize);
  4730.  
  4731.     /* Check the window exists */
  4732.     if (theWindowToken==nil)
  4733.         myErr = errAEIllegalIndex;
  4734.     else {
  4735.  
  4736.         /* get the property - it's in the selection data */
  4737.  
  4738.         myErr = AECoerceDesc(selectionData, typeType, &propDesc);
  4739.         GetRawDataFromDescriptor(&propDesc, (Ptr)&theProperty, sizeof(theProperty), &actualSize);
  4740.         /*
  4741.             Combine the two into single token
  4742.         */
  4743.         myWindowProp.tokenWindowToken = theWindowToken;
  4744.         myWindowProp.tokenProperty    = theProperty;
  4745.  
  4746.         myErr = AECreateDesc(typeMyWindowProp, (Ptr)&myWindowProp, sizeof(myWindowProp), value);
  4747.     }
  4748.  
  4749.     if (windowDesc.dataHandle)
  4750.         ignoreErr = AEDisposeDesc(&windowDesc);
  4751.  
  4752.     if (propDesc.dataHandle)
  4753.         ignoreErr = AEDisposeDesc(&propDesc);
  4754.  
  4755.     return myErr;
  4756. }    /* PropertyFromWndwAccessor */
  4757.  
  4758. pascal OSErr PropertyFromApplAccessor(
  4759.     DescType     wantClass,
  4760.     const AEDesc *container,
  4761.     DescType     containerClass,
  4762.     DescType     form,
  4763.     const AEDesc *selectionData,
  4764.     AEDesc       *value,
  4765.     long         theRefCon)
  4766. {
  4767. #pragma unused (theRefCon, containerClass)
  4768.  
  4769.     OSErr         myErr;
  4770.     OSErr         ignoreErr;
  4771.     appToken      theApplToken;
  4772.     DescType      theProperty;
  4773.     AEDesc        applDesc;
  4774.     AEDesc        propDesc;
  4775.     Size          actualSize;
  4776.     applPropToken myApplProp;
  4777.  
  4778.     value->dataHandle     = nil;
  4779.     applDesc.dataHandle   = nil;
  4780.     propDesc.dataHandle   = nil;
  4781.  
  4782.     if ((wantClass != cProperty) || (form != formPropertyID)) {
  4783.         return errAEWrongDataType;
  4784.     }
  4785.  
  4786.     /* get the application token - it's the container */
  4787.  
  4788.     myErr = AECoerceDesc(container, typeMyAppl, &applDesc);
  4789.     GetRawDataFromDescriptor(&applDesc, (Ptr)&theApplToken, sizeof(theApplToken), &actualSize);
  4790.  
  4791.     /* get the property - it's in the selection data */
  4792.  
  4793.     myErr = AECoerceDesc(selectionData, typeType, &propDesc);
  4794.     GetRawDataFromDescriptor(&propDesc, (Ptr)&theProperty, sizeof(theProperty), &actualSize);
  4795.     /*
  4796.         Combine the two into single token
  4797.     */
  4798.     myApplProp.tokenApplToken    = theApplToken;
  4799.     myApplProp.tokenApplProperty = theProperty;
  4800.  
  4801.     myErr = AECreateDesc(typeMyApplProp, (Ptr)&myApplProp, sizeof(myApplProp), value);
  4802.  
  4803.     if (applDesc.dataHandle)
  4804.         ignoreErr = AEDisposeDesc(&applDesc);
  4805.  
  4806.     if (propDesc.dataHandle)
  4807.         ignoreErr = AEDisposeDesc(&propDesc);
  4808.  
  4809.     return myErr;
  4810. }    /* PropertyFromApplAccessor */
  4811.  
  4812. pascal OSErr MenuNameToMenuToken(const Str255 theName, MenuToken *theToken)
  4813. {
  4814.     short   index;
  4815.  
  4816.     for (index=appleM; index<kLastMenu; index++) {
  4817.         if (IUEqualString(theName, (**(myMenus[index])).menuData)==0) {
  4818.             theToken->theTokenMenu = myMenus[index];
  4819.             theToken->theTokenID   = index+appleID;
  4820.             return noErr;
  4821.         }
  4822.     }
  4823.     return errAEIllegalIndex;
  4824. }
  4825.  
  4826. pascal OSErr MenuFromNullAccessor(
  4827.     DescType      wantClass,
  4828.     const AEDesc  *container,
  4829.     DescType      containerClass,
  4830.     DescType      form,
  4831.     const AEDesc  *selectionData,
  4832.     AEDesc        *value,
  4833.     long          theRefCon)
  4834. {
  4835. #pragma unused (container,theRefCon)
  4836.  
  4837.     OSErr       myErr;
  4838.     Str255      nameStr;
  4839.     MenuToken   theMenu;
  4840.     short       index;
  4841.     AEDesc      resultDesc;
  4842.  
  4843.     myErr = errAEBadKeyForm;    /* or whatever */
  4844.  
  4845.     value->dataHandle     = nil;
  4846.     resultDesc.dataHandle = nil;
  4847.  
  4848.     /*
  4849.         should only be called with wantClass = cMenu and
  4850.         with containerClass = typeNull or typeMyAppl.
  4851.         Currently accept as either formName or formAbsolutePosition
  4852.     */
  4853.  
  4854.     if (
  4855.         (wantClass != cMenu) ||
  4856.         ((containerClass != typeNull) && (containerClass != typeMyAppl)) ||
  4857.         !((form == formName) || (form == formAbsolutePosition))
  4858.     )
  4859.         return errAEWrongDataType;
  4860.  
  4861.     if (form == formName) {
  4862.         myErr = GetPStringFromDescriptor(selectionData, (char *)nameStr);
  4863.         myErr = MenuNameToMenuToken(nameStr, &theMenu);
  4864.     }
  4865.  
  4866.     if (form == formAbsolutePosition) {
  4867.         myErr     = GetIntegerFromDescriptor(selectionData, &index);
  4868.         if (index<0)
  4869.             index = kLastMenu + index + 1;
  4870.  
  4871.         if (index>0 && index<kLastMenu+1) {
  4872.             theMenu.theTokenMenu = myMenus[index-1];
  4873.             theMenu.theTokenID   = index-1+appleID;
  4874.         } else
  4875.             myErr = errAEIllegalIndex;    /* or whatever */
  4876.     }
  4877.  
  4878.     if (myErr == noErr)
  4879.         myErr = AECreateDesc(typeMyMenu, (Ptr)&theMenu, sizeof(theMenu), value);
  4880.  
  4881.     return myErr;
  4882. }    /* MenuFromNullAccessor */
  4883.  
  4884. pascal OSErr PropertyFromMenuAccessor(
  4885.     DescType     wantClass,
  4886.     const AEDesc *container,
  4887.     DescType     containerClass,
  4888.     DescType     form,
  4889.     const AEDesc *selectionData,
  4890.     AEDesc       *value,
  4891.     long         theRefCon)
  4892. {
  4893. #pragma unused (theRefCon, containerClass)
  4894.  
  4895.     OSErr         myErr;
  4896.     OSErr         ignoreErr;
  4897.     MenuToken     theMenuToken;
  4898.     DescType      theProperty;
  4899.     AEDesc        menuDesc;
  4900.     AEDesc        propDesc;
  4901.     Size          actualSize;
  4902.     MenuPropToken myMenuProp;
  4903.  
  4904.     value->dataHandle     = nil;
  4905.     menuDesc.dataHandle   = nil;
  4906.     propDesc.dataHandle   = nil;
  4907.  
  4908.     if ((wantClass != cProperty) || (form != formPropertyID)) {
  4909.         return errAEWrongDataType;
  4910.     }
  4911.  
  4912.     /* get the menu token - it's the container */
  4913.  
  4914.     myErr = AECoerceDesc(container, typeMyMenu, &menuDesc);
  4915.     GetRawDataFromDescriptor(&menuDesc, (Ptr)&theMenuToken, sizeof(theMenuToken), &actualSize);
  4916.  
  4917.     /* get the property - it's in the selection data */
  4918.  
  4919.     myErr = AECoerceDesc(selectionData, typeType, &propDesc);
  4920.     GetRawDataFromDescriptor(&propDesc, (Ptr)&theProperty, sizeof(theProperty), &actualSize);
  4921.  
  4922.     /*
  4923.         Combine the two into single token
  4924.     */
  4925.     myMenuProp.theMenuToken = theMenuToken;
  4926.     myMenuProp.theMenuProp  = theProperty;
  4927.  
  4928.     myErr = AECreateDesc(typeMyMenuProp, (Ptr)&myMenuProp, sizeof(myMenuProp), value);
  4929.  
  4930.     if (menuDesc.dataHandle)
  4931.         ignoreErr = AEDisposeDesc(&menuDesc);
  4932.  
  4933.     if (propDesc.dataHandle)
  4934.         ignoreErr = AEDisposeDesc(&propDesc);
  4935.  
  4936.     return myErr;
  4937. }    /* PropertyFromMenuAccessor */
  4938.  
  4939. pascal OSErr PropertyFromMenuItemAccessor(
  4940.     DescType     wantClass,
  4941.     const AEDesc *container,
  4942.     DescType     containerClass,
  4943.     DescType     form,
  4944.     const AEDesc *selectionData,
  4945.     AEDesc       *value,
  4946.     long         theRefCon)
  4947. {
  4948. #pragma unused (theRefCon, containerClass)
  4949.  
  4950.     OSErr         myErr;
  4951.     OSErr         ignoreErr;
  4952.     MenuItemToken theMenuItemToken;
  4953.     DescType      theProperty;
  4954.     AEDesc        itemDesc;
  4955.     AEDesc        propDesc;
  4956.     Size          actualSize;
  4957.     MenuItemPropToken myItemProp;
  4958.  
  4959.     value->dataHandle     = nil;
  4960.     itemDesc.dataHandle   = nil;
  4961.     propDesc.dataHandle   = nil;
  4962.  
  4963.     if ((wantClass != cProperty) || (form != formPropertyID)) {
  4964.         return errAEWrongDataType;
  4965.     }
  4966.  
  4967.     /* get the menu token - it's the container */
  4968.  
  4969.     myErr = AECoerceDesc(container, typeMyMenuItem, &itemDesc);
  4970.     GetRawDataFromDescriptor(&itemDesc, (Ptr)&theMenuItemToken, sizeof(theMenuItemToken), &actualSize);
  4971.  
  4972.     /* get the property - it's in the selection data */
  4973.  
  4974.     myErr = AECoerceDesc(selectionData, typeType, &propDesc);
  4975.     GetRawDataFromDescriptor(&propDesc, (Ptr)&theProperty, sizeof(theProperty), &actualSize);
  4976.     /*
  4977.         Combine the two into single token
  4978.     */
  4979.     myItemProp.theItemToken  = theMenuItemToken;
  4980.     myItemProp.theItemProp   = theProperty;
  4981.  
  4982.     myErr = AECreateDesc(typeMyItemProp, (Ptr)&myItemProp, sizeof(myItemProp), value);
  4983.  
  4984.     if (itemDesc.dataHandle)
  4985.         ignoreErr = AEDisposeDesc(&itemDesc);
  4986.  
  4987.     if (propDesc.dataHandle)
  4988.         ignoreErr = AEDisposeDesc(&propDesc);
  4989.  
  4990.     return myErr;
  4991. }    /* PropertyFromMenuItemAccessor */
  4992.  
  4993. pascal OSErr ItemNameToItemIndex(const Str255 theName, MenuHandle theMenu, short *theIndex)
  4994. {
  4995.     short   index;
  4996.     short   maxItems;
  4997.     Str255  menuName;
  4998.  
  4999.     maxItems = CountMItems(theMenu);
  5000.  
  5001.     for (index=1; index<=maxItems; index++) {
  5002.         GetItem(theMenu, index, menuName);
  5003.         if (IUEqualString(theName, menuName)==0) {
  5004.             *theIndex = index;
  5005.             return noErr;
  5006.         }
  5007.     }
  5008.     return errAEIllegalIndex;
  5009. }
  5010.  
  5011. pascal OSErr MenuItemFromMenuAccessor(
  5012.     DescType     wantClass,
  5013.     const AEDesc *container,
  5014.     DescType     containerClass,
  5015.     DescType     form,
  5016.     const AEDesc *selectionData,
  5017.     AEDesc       *value,
  5018.     long         theRefCon)
  5019. {
  5020. #pragma unused (theRefCon)
  5021.  
  5022.     OSErr         myErr;
  5023.     OSErr         ignoreErr;
  5024.     MenuItemToken theMenuItemToken;
  5025.     MenuToken     theMenuToken;
  5026.     AEDesc        menuDesc;
  5027.     Size          actualSize;
  5028.     Str255        nameStr;
  5029.     short         maxItems;
  5030.     short         index;
  5031.  
  5032.     value->dataHandle     = nil;
  5033.     menuDesc.dataHandle   = nil;
  5034.  
  5035.     if (
  5036.         (wantClass != cMenuItem) || (containerClass != cMenu) ||
  5037.         ((form != formAbsolutePosition) && (form != formName))
  5038.     ) {
  5039.         return errAEWrongDataType;
  5040.     }
  5041.  
  5042.     /* get the menu token - it's the container */
  5043.  
  5044.     myErr = AECoerceDesc(container, typeMyMenu, &menuDesc);
  5045.     GetRawDataFromDescriptor(&menuDesc, (Ptr)&theMenuToken, sizeof(theMenuToken), &actualSize);
  5046.  
  5047.     if (form==formAbsolutePosition) {
  5048.         myErr = GetIntegerFromDescriptor(selectionData, &index);
  5049.         maxItems = CountMItems(theMenuToken.theTokenMenu);
  5050.  
  5051.         if (index<0)
  5052.             index = maxItems + index + 1;
  5053.  
  5054.         if ((index<1) || (index>maxItems))
  5055.           myErr = errAEIllegalIndex;
  5056.     }
  5057.  
  5058.     if (form == formName) {
  5059.         myErr  = GetPStringFromDescriptor(selectionData, (char *)nameStr);
  5060.         myErr  = ItemNameToItemIndex(nameStr, theMenuToken.theTokenMenu, &index);
  5061.     }
  5062.  
  5063.     /*
  5064.         Combine the two into single token
  5065.     */
  5066.  
  5067.     theMenuItemToken.theMenuToken  = theMenuToken;
  5068.     theMenuItemToken.theTokenItem  = index;
  5069.  
  5070.     if (myErr==noErr)
  5071.         myErr = AECreateDesc(typeMyMenuItem, (Ptr)&theMenuItemToken, sizeof(theMenuItemToken), value);
  5072.  
  5073.     if (menuDesc.dataHandle)
  5074.         ignoreErr = AEDisposeDesc(&menuDesc);
  5075.  
  5076.     return myErr;
  5077. }    /* MenuItemFromMenuAccessor */
  5078.  
  5079. /*******************************************************************************/
  5080. /*
  5081.     Stuff for counting objects
  5082. */
  5083.  
  5084. pascal OSErr MyCountProc(
  5085.     DescType     desiredType,
  5086.     DescType     containerClass,
  5087.     const AEDesc *container,
  5088.     long         *result)
  5089. /* so far all I count is:
  5090.   (1) the number of active windows in the app;
  5091.   (2) the number of words in a window
  5092. */
  5093. {
  5094.     OSErr       myErr;
  5095.     WindowToken theWindowToken;
  5096.     DPtr        theDocument;
  5097.     TEHandle    theHTE;
  5098.     AEDesc      newDesc;
  5099.     short       wordStart;
  5100.     short       wordLength;
  5101.     Size        tokenSize;
  5102.     TextToken   theTextToken;
  5103.  
  5104.     *result = -1;    /* easily recognized illegal value */
  5105.  
  5106.     myErr = errAEWrongDataType;
  5107.  
  5108.     if (desiredType == cWindow) {
  5109.         if ((containerClass == typeNull) || (containerClass == cApplication))
  5110.             *result = CountWindows();
  5111.     }
  5112.  
  5113.     if ((desiredType == cWord) || (desiredType == cLine) || (desiredType == cChar)) {
  5114.         myErr = AECoerceDesc(container, typeMyWndw, &newDesc);
  5115.         if (newDesc.descriptorType!=typeNull) {
  5116.                 GetRawDataFromDescriptor(
  5117.                     &newDesc,
  5118.                     (Ptr)&theWindowToken,
  5119.                     sizeof(theWindowToken),
  5120.                     &tokenSize);
  5121.  
  5122.                 myErr = AEDisposeDesc(&newDesc);
  5123.  
  5124.                 if (theWindowToken==nil)
  5125.                     myErr = errAEIllegalIndex;
  5126.                 else {
  5127.                     theDocument = DPtrFromWindowPtr(theWindowToken);
  5128.                     theHTE      = theDocument->theText;
  5129.  
  5130.                     if (desiredType == cWord) {
  5131.                         wordStart   = 0;
  5132.                         wordLength  = (**theHTE).teLength;
  5133.                         *result     = CountWords(theHTE, wordStart, wordLength);
  5134.                     }
  5135.  
  5136.                     if (desiredType == cChar)
  5137.                         *result = (**theHTE).teLength;
  5138.  
  5139.                     if (desiredType == cLine)
  5140.                         *result = CountLines(theHTE);
  5141.                 }
  5142.             }
  5143.  
  5144.         myErr = AECoerceDesc(container, typeMyText, &newDesc);
  5145.         if (newDesc.descriptorType!=typeNull) {
  5146.             GetRawDataFromDescriptor(
  5147.                 &newDesc,
  5148.                 (Ptr)&theTextToken,
  5149.                 sizeof(theTextToken),
  5150.                 &tokenSize);
  5151.  
  5152.             myErr = AEDisposeDesc(&newDesc);
  5153.  
  5154.             theDocument = DPtrFromWindowPtr(theTextToken.tokenWindow);
  5155.             theHTE      = theDocument->theText;
  5156.  
  5157.             if (desiredType == cWord) {
  5158.                 wordStart   = theTextToken.tokenOffset-1;
  5159.                 wordLength  = theTextToken.tokenLength;
  5160.                 *result     = CountWords(theHTE, wordStart, wordLength);
  5161.             }
  5162.  
  5163.             if (desiredType == cChar)
  5164.                 *result = theTextToken.tokenLength;
  5165.  
  5166.             if (desiredType == cLine)
  5167.                 *result    =
  5168.                     LineOfOffset(theHTE,theTextToken.tokenOffset-1) -
  5169.                     LineOfOffset(theHTE,theTextToken.tokenOffset+theTextToken.tokenLength-1)
  5170.                     +1;
  5171.         }
  5172.     }
  5173.  
  5174.     return myErr;
  5175. }    /* MyCountProc */
  5176.  
  5177. /*******************************************************************************/
  5178. /*
  5179.     Coercion Handlers - Allow AEResolve to do the hard work
  5180. */
  5181. pascal OSErr CoerceObjToAnything(
  5182.     const AEDesc *theAEDesc,
  5183.     DescType     toType,
  5184.     long         handlerRefCon,
  5185.     AEDesc       *result)
  5186. /*
  5187.     CoerceObjToAnything functions by using AEResolve to do the hard
  5188.     work.
  5189. */
  5190. {
  5191. #pragma unused (handlerRefCon)
  5192.  
  5193.     OSErr  myErr;
  5194.     AEDesc objDesc;
  5195.  
  5196.     myErr = errAECoercionFail;
  5197.  
  5198.     result->dataHandle = nil;
  5199.     objDesc.dataHandle = nil;
  5200.  
  5201.  
  5202.     if (theAEDesc->descriptorType != typeObjectSpecifier) {
  5203.         return errAEWrongDataType;
  5204.     }
  5205.  
  5206.     /* resolve the object specifier */
  5207.     myErr = AEResolve(theAEDesc, kAEIDoMinimum, &objDesc);
  5208.  
  5209.     /* hopefully it's the right type by now, but we'll give it a nudge */
  5210.     if (myErr==noErr) {
  5211.         myErr = AECoerceDesc(&objDesc, toType, result);
  5212.         myErr = AEDisposeDesc(&objDesc);
  5213.     }
  5214.  
  5215.     if (result->descriptorType!=toType) {
  5216.         /*DebugStr('COTA - Not of requested type');*/
  5217.     }
  5218.  
  5219.     return myErr;
  5220. }    /* CoerceObjToAnything */
  5221.  
  5222. /*******************************************************************************/
  5223.  
  5224. /*----------------------------------------------------------------------------------------------*/
  5225.  
  5226. /*now for the edition manager event handling code*/
  5227.  
  5228. pascal OSErr GetHandleFromEvent(const AppleEvent *theAppleEvent, SectionHandle *sectionH)
  5229. {
  5230.     DescType ignoreType;
  5231.     Size          ignoreSize;
  5232.  
  5233.     return
  5234.         AEGetKeyPtr(
  5235.             theAppleEvent,
  5236.             keyDirectObject,
  5237.             typeSectionH,
  5238.             &ignoreType,
  5239.             (Ptr)sectionH,
  5240.             sizeof(SectionHandle),
  5241.             &ignoreSize);
  5242. } /* GetHandleFromEvent */
  5243.  
  5244. /*----------------------------------------------------------------------------------------------*/
  5245. pascal OSErr DoReadSection(const AppleEvent *theAppleEvent,AppleEvent *reply,long refCon)
  5246. {
  5247. #pragma unused (reply, refCon)
  5248.  
  5249.     OSErr         err;
  5250.     SectionHandle sectionH;
  5251.  
  5252.     err = GetHandleFromEvent(theAppleEvent, §ionH);
  5253.     if (IsRegisteredSection(sectionH)==noErr)
  5254.         ReadAnEdition(sectionH);
  5255.     return err;
  5256. } /* DoReadSection */
  5257.  
  5258. /*----------------------------------------------------------------------------------------------*/
  5259. pascal OSErr DoWriteSection(const AppleEvent *theAppleEvent, AppleEvent *reply, long refCon)
  5260. {
  5261. #pragma unused (reply, refCon)
  5262.  
  5263.     OSErr         err;
  5264.     SectionHandle sectionH;
  5265.  
  5266.     err = GetHandleFromEvent(theAppleEvent, §ionH);
  5267.     if (IsRegisteredSection(sectionH) == noErr)
  5268.         WriteAnEdition(sectionH);
  5269.  
  5270.     return err;
  5271. } /* DoWriteSection */
  5272.  
  5273. /*----------------------------------------------------------------------------------------------*/
  5274. pascal OSErr DoScrollSection(const AppleEvent *theAppleEvent, AppleEvent *reply, long refCon)
  5275. {
  5276. #pragma unused (reply, refCon)
  5277.  
  5278.     OSErr         err;
  5279.     SectionHandle sectionH;
  5280.     SectHandle    aSectHandle;
  5281.  
  5282.     err = GetHandleFromEvent(theAppleEvent, §ionH);
  5283.     /*get at the sectHandle*/
  5284.     aSectHandle = (SectHandle)GetERefCon(sectionH);
  5285.     TESetSelect((**aSectHandle).fStart, (**aSectHandle).fEnd, ((**aSectHandle).fDocument)->theText);
  5286.     ShowSelect((**aSectHandle).fDocument);
  5287.     return err;
  5288. }
  5289.  
  5290. /*----------------------------------------------------------------------------------------------*/
  5291. pascal OSErr DoCancelSection(const AppleEvent *theAppleEvent, AppleEvent *reply, long refCon)
  5292. {
  5293. #pragma unused (reply, refCon)
  5294.  
  5295.     OSErr         err;
  5296.     SectionHandle sectionH;
  5297.     SectHandle    aSectHandle;
  5298.  
  5299.     err = GetHandleFromEvent(theAppleEvent, §ionH);
  5300.     aSectHandle = (SectHandle)GetERefCon(sectionH);
  5301.     err = UnRegisterSection(sectionH);
  5302.     DeleteASection(aSectHandle, (**aSectHandle).fDocument);
  5303.     return noErr;
  5304. } /* DoCancelSection */
  5305.  
  5306. #endif
  5307.  
  5308. pascal OSErr Text2FSSpec(
  5309.     DescType type, Ptr path, Size size, 
  5310.     DescType to, long refCon, AEDesc * result)
  5311. {
  5312.     OSErr            err;
  5313.     char            file[256];
  5314.     FSSpec        spec;
  5315.     CInfoPBRec    info;
  5316.     
  5317.     if (size > 255)
  5318.         return errAECoercionFail;
  5319.         
  5320.     memcpy(file, path, size);
  5321.     file[size] = 0;
  5322.     
  5323.     if (err = Path2FSSpec(file, &spec))
  5324.         return err;
  5325.     if (err = FSpCatInfo(&spec, &info))
  5326.         return err;
  5327.     
  5328.     return AECreateDesc(typeFSS, (Ptr) &spec, sizeof(FSSpec), result);
  5329. }
  5330.  
  5331. /* -----------------------------------------------------------------------
  5332.         Name:             InitAppleEvents
  5333.         Purpose:        Initialise the AppleEvent despatch table
  5334.      -----------------------------------------------------------------------**/
  5335.  
  5336. #pragma segment Main
  5337.  
  5338. #define noRefCon -1
  5339.  
  5340. pascal void InitAppleEvents(void)
  5341. {
  5342.     OSErr aevtErr;
  5343.  
  5344.      gBigBrother = 0;
  5345.     gCharsInBuffer = 0;
  5346.     gTypingBuffer  = (char *)NewPtr(32000);
  5347.     gTypingTargetObject.dataHandle = 0;
  5348.  
  5349.     /*set up the dispatch table for the four standard apple events*/
  5350.  
  5351.     aevtErr = AEInstallEventHandler( kCoreEventClass, kAEOpenApplication, (EventHandlerProcPtr)DoOpenApp, noRefCon, false) ;
  5352.     aevtErr = AEInstallEventHandler( kCoreEventClass, kAEOpenDocuments,   (EventHandlerProcPtr)DoOpenDocument, noRefCon, false) ;
  5353.     aevtErr = AEInstallEventHandler( kCoreEventClass, kAEPrintDocuments,  (EventHandlerProcPtr)DoPrintDocuments, noRefCon, false) ;
  5354.     aevtErr = AEInstallEventHandler( kCoreEventClass, kAEQuitApplication, (EventHandlerProcPtr)MyQuit, noRefCon, false) ;
  5355.  
  5356. #ifndef RUNTIME
  5357.     aevtErr = AEInstallEventHandler( MPAppSig,           kAEOpenDocuments,   (EventHandlerProcPtr)DoOpenDocument, 1, false) ;
  5358.  
  5359.     /* set up the dispatch table for the core AppleEvents for text */
  5360.  
  5361.     aevtErr = AEInstallEventHandler( kAECoreSuite,     kAEDelete, (EventHandlerProcPtr)DoDeleteEdit,noRefCon, false);
  5362.  
  5363.     aevtErr = AEInstallEventHandler( kAEMiscStandards, kAECut,    (EventHandlerProcPtr)DoCutEdit,   noRefCon, false);
  5364.     aevtErr = AEInstallEventHandler( kAEMiscStandards, kAECopy,   (EventHandlerProcPtr)DoCopyEdit,  noRefCon, false);
  5365.     aevtErr = AEInstallEventHandler( kAEMiscStandards, kAEPaste,  (EventHandlerProcPtr)DoPasteEdit, noRefCon, false);
  5366.     aevtErr = AEInstallEventHandler( kAECoreSuite,     kAESetData,(EventHandlerProcPtr)DoSetData,   noRefCon, false);
  5367.     aevtErr = AEInstallEventHandler( kAECoreSuite,     kAEGetData,(EventHandlerProcPtr)DoGetData,   noRefCon, false);
  5368.     aevtErr = AEInstallEventHandler( kAECoreSuite,     kAEGetDataSize,(EventHandlerProcPtr)DoGetDataSize,   noRefCon, false);
  5369.  
  5370.     aevtErr = AEInstallEventHandler( kAECoreSuite, kAECountElements,   (EventHandlerProcPtr)HandleNumberOfElements,   noRefCon, false);
  5371.     aevtErr = AEInstallEventHandler( kAECoreSuite, kAECreateElement,   (EventHandlerProcPtr)DoNewElement,   noRefCon, false);
  5372.     aevtErr = AEInstallEventHandler( kAECoreSuite, kAEDoObjectsExist,  (EventHandlerProcPtr)DoIsThereA,   noRefCon, false);
  5373.  
  5374.     aevtErr = AEInstallEventHandler( kAECoreSuite,     kAEClose,  (EventHandlerProcPtr)DoCloseWindow,noRefCon, false);
  5375.     aevtErr = AEInstallEventHandler( kAECoreSuite,     kAESave,   (EventHandlerProcPtr)DoSaveWindow,noRefCon, false);
  5376.     aevtErr = AEInstallEventHandler( kAEMiscStandards, kAERevert, (EventHandlerProcPtr)DoRevertWindow,noRefCon, false);
  5377.  
  5378.     aevtErr = AEInstallEventHandler( kAEMiscStandards, kAECreatePublisher,    (EventHandlerProcPtr)HandleCreatePub, noRefCon, false);
  5379.     aevtErr = AEInstallEventHandler( kAEMiscStandards, kAEMakeObjectsVisible, (EventHandlerProcPtr)HandleShowSelection,   noRefCon, false);
  5380.  
  5381.     aevtErr = AEInstallEventHandler( kAEMiscStandards, kAEDoScript,           (EventHandlerProcPtr)DoScript, noRefCon, false);
  5382.  
  5383.     /* Now look for recording notifications */
  5384.  
  5385.     aevtErr = AEInstallEventHandler( kCoreEventClass, kAEStartedRecording, (EventHandlerProcPtr)HandleStartRecording, noRefCon, false);
  5386.     aevtErr = AEInstallEventHandler( kCoreEventClass, kAEStoppedRecording, (EventHandlerProcPtr)HandleStopRecording, noRefCon, false);
  5387.  
  5388.     /* Now Put in the required object accessors */
  5389.  
  5390.     aevtErr = AESetObjectCallbacks(nil,MyCountProc,nil,nil,nil,nil,nil);
  5391.  
  5392.     aevtErr = AEInstallObjectAccessor(cWindow,      typeNull,   WindowFromNullAccessor,  0,false);
  5393.     aevtErr = AEInstallObjectAccessor(cWindow,      typeMyAppl, WindowFromNullAccessor,  0,false);
  5394.  
  5395.     aevtErr = AEInstallObjectAccessor(cApplication, typeNull,   ApplicationFromNullAccessor,  0,false);
  5396.     aevtErr = AEInstallObjectAccessor(cProperty,    typeMyAppl, PropertyFromApplAccessor,0,false);
  5397.  
  5398.     aevtErr = AEInstallObjectAccessor(cChar,             typeMyWndw,TextElemFromWndwAccessor,0,false);
  5399.     aevtErr = AEInstallObjectAccessor(cSpot,             typeMyWndw,TextElemFromWndwAccessor,0,false);
  5400.     aevtErr = AEInstallObjectAccessor(cWord,             typeMyWndw,TextElemFromWndwAccessor,0,false);
  5401.     aevtErr = AEInstallObjectAccessor(cLine,             typeMyWndw,TextElemFromWndwAccessor,0,false);
  5402.     aevtErr = AEInstallObjectAccessor(cText,             typeMyWndw,TextElemFromWndwAccessor,0,false);
  5403.     aevtErr = AEInstallObjectAccessor(cProperty,        typeMyWndw,PropertyFromWndwAccessor,0,false);
  5404.  
  5405.     aevtErr = AEInstallObjectAccessor(cProperty,          typeMyText,PropertyFromTextAccessor,0,false);
  5406.     aevtErr = AEInstallObjectAccessor(cChar,           typeMyText,TextElemFromTextAccessor,0,false);
  5407.     aevtErr = AEInstallObjectAccessor(cWord,           typeMyText,TextElemFromTextAccessor,0,false);
  5408.     aevtErr = AEInstallObjectAccessor(cSpot,           typeMyText,TextElemFromTextAccessor,0,false);
  5409.     aevtErr = AEInstallObjectAccessor(cLine,           typeMyText,TextElemFromTextAccessor,0,false);
  5410.  
  5411.     aevtErr = AEInstallObjectAccessor(cMenu,           typeNull,       MenuFromNullAccessor,    0,false);
  5412.     aevtErr = AEInstallObjectAccessor(cProperty,        typeMyMenu,     PropertyFromMenuAccessor,0,false);
  5413.     aevtErr = AEInstallObjectAccessor(cProperty,        typeMyMenuItem, PropertyFromMenuItemAccessor,0,false);
  5414.     aevtErr = AEInstallObjectAccessor(cMenuItem,        typeMyMenu,     MenuItemFromMenuAccessor,0,false);
  5415.     /* Now the coercion handlers */
  5416.  
  5417.     aevtErr = AEInstallCoercionHandler(typeObjectSpecifier,typeMyAppl,      (ProcPtr)CoerceObjToAnything,0,true,false);
  5418.     aevtErr = AEInstallCoercionHandler(typeObjectSpecifier,typeMyWndw,      (ProcPtr)CoerceObjToAnything,0,true,false);
  5419.     aevtErr = AEInstallCoercionHandler(typeObjectSpecifier,typeMyText,      (ProcPtr)CoerceObjToAnything,0,true,false);
  5420.     aevtErr = AEInstallCoercionHandler(typeObjectSpecifier,typeMyTextProp,  (ProcPtr)CoerceObjToAnything,0,true,false);
  5421.     aevtErr = AEInstallCoercionHandler(typeObjectSpecifier,typeMyWindowProp,(ProcPtr)CoerceObjToAnything,0,true,false);
  5422.     aevtErr = AEInstallCoercionHandler(typeObjectSpecifier,typeMyApplProp,  (ProcPtr)CoerceObjToAnything,0,true,false);
  5423.     aevtErr = AEInstallCoercionHandler(typeObjectSpecifier,typeMyMenu,      (ProcPtr)CoerceObjToAnything,0,true,false);
  5424.     aevtErr = AEInstallCoercionHandler(typeObjectSpecifier,typeMyMenuProp,  (ProcPtr)CoerceObjToAnything,0,true,false);
  5425.     aevtErr = AEInstallCoercionHandler(typeObjectSpecifier,typeMyMenuItem,  (ProcPtr)CoerceObjToAnything,0,true,false);
  5426.     aevtErr = AEInstallCoercionHandler(typeObjectSpecifier,typeMyItemProp,  (ProcPtr)CoerceObjToAnything,0,true,false);
  5427.  
  5428.     aevtErr = AEInstallCoercionHandler(typeChar,typeFSS,  (ProcPtr)Text2FSSpec,0,false,false);
  5429.         /*now install the appropriate edition manager events*/
  5430.  
  5431.     aevtErr = AEInstallEventHandler( sectionEventMsgClass, sectionReadMsgID,   (EventHandlerProcPtr)DoReadSection, noRefCon, false) ;
  5432.     aevtErr = AEInstallEventHandler( sectionEventMsgClass, sectionWriteMsgID,  (EventHandlerProcPtr)DoWriteSection, noRefCon, false) ;
  5433.     aevtErr = AEInstallEventHandler( sectionEventMsgClass, sectionScrollMsgID, (EventHandlerProcPtr)DoScrollSection, noRefCon, false) ;
  5434.     aevtErr = AEInstallEventHandler( sectionEventMsgClass, sectionCancelMsgID, (EventHandlerProcPtr)DoCancelSection, noRefCon, false) ;
  5435. #endif
  5436. } /* InitAppleEvents */
  5437.